问题的描述:
http://www.programfan.com/club/showbbs.asp?id=73789
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define N 30 /*27<=N<=127*/
#define NULL 0
typedef struct Qnode /*定义队列的接点类型*/
{
int data;
struct Qnode *next;
}Qnode,*Queueptr;
Queueptr p,q;
typedef struct
{
Queueptr front;
Queueptr rear;
}linkQueue;
linkQueue *Q; /*定义队列*/
typedef struct stack4 /*定义分钟栈*/
{
Queueptr top;
Queueptr base;
}stack4;
stack4 *min;
typedef struct stack11 /*定义5分钟栈类型(小时栈类型)*/
{
Queueptr top;
Queueptr base;
}stack11;
stack11 *fmin,*hour; /*建立五分钟和小时栈*/
int minute;
void initQueue(linkQueue *O) /*队列的初始化*/
{
int i=1,j;
Queueptr head,new;
head=(Qnode *)malloc(sizeof(Qnode));
if(!head)
{
printf("failture for applying a area!\n");
exit(0);
}
O->front=p=head;
head->data=i;
head->next=NULL;
while(i<N)
{
new=(Qnode *)malloc(sizeof(Qnode));
if(!new)
{
printf("failture for applying a area!\n");
exit(0);
}
head->next=new;
head=head->next;
head->data=++i;
}
head->next=NULL;
O->rear=head;
}
main() /*主函数*/
{
int day=1,j=1; /*天数*/
initQueue(Q);
while(1) /*循环*/
{
for(minute=1;minute<1440;minute++)
{
Q->front=Q->front->next;
p->next=NULL;
min->base=min->top=p;
min->top++;
p=Q->front;
if(minute%4==0)
{
Q->front=Q->front->next;
p->next=NULL;
min->top=p;
min->top++;
p=Q->front;
min->top--;
Q->rear->next=min->top;
min->top--;
}
if(minute%5==0)
{
Q->front=Q->front->next;
p->next=NULL;
fmin->base=fmin->top=p;
fmin->top++;
p=Q->front;
fmin->top--;
Q->rear->next=min->top;
fmin->top--;
}
if(minute%60==0)
{
Q->front=Q->front->next;
p->next=NULL;
hour->base=hour->top=p;
hour->top++;
p=Q->front;
hour->top--;
Q->rear->next=hour->top;
hour->top--;
}
}
if(Q->front->data==1) /*测试是否符合条件,若符合跳出,否则继续循环下去*/
{
while(Q->front->data==(Q->front->next->data+1)&&j<=N)
{
Q->front=Q->front->next;
j++;
}
if(Q->front->data==N)
break;
}
day++;
}
printf("after %d days,the condition will return the original!",day);/*输出天数*/
}
正文
球钟问题(栈和队列的应用)2005-05-04 13:01:00
【评论】 【打印】 【字体:大 中 小】 本文链接:http://blog.pfan.cn/jay0518/852.html
阅读(3377) | 评论(0)
版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!
评论