球钟问题(栈和队列的应用) |
|
www.nanhushi.com 佚名 不详 |
#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);/*输出天数*/
|
|
|
文章录入:杜斌 责任编辑:杜斌 |
|
上一篇文章: C语言辅导:C语言随机函数 下一篇文章: 俄罗斯方块程序的另类算法 |
【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |
|
|