#include <stdio.h>
#include <stdlib.h>
#define N 28
#define M 16
#define M_1 4
#define M_5 11
#define M_60 11
typedef int datatype;
typedef struct
{
datatype data[N];
int front, rear;
}sequeue;
typedef struct
{
datatype data[M];
int top;
}sqstack;
sqstack *CreateSqstack()
{
sqstack *sq;
sq = (sqstack *)malloc(sizeof(sqstack));
sq->top = -1;
return sq;
}
int EmptySqstack(sqstack *sq)
{
return (-1 == sq->top);
}
int FullSqstack(sqstack *sq)
{
return (N-1 == sq->top);
}
void PushSqstack(sqstack *sq, datatype x)
{
sq->top++;
sq->data[sq->top] = x;
}
datatype PopSqstack(sqstack *sq)
{
return (sq->data[sq->top--]);
}
datatype GetTop(sqstack *sq)
{
return sq->data[sq->top];
}
sequeue *CreateSequeue()
{
sequeue *sq;
sq = (sequeue*)malloc(sizeof(sequeue));
sq->front = sq->rear = 0;
return sq;
}
int EmptySequeue(sequeue *sq)
{
return (sq->front == sq->rear);
}
int FullSequeue(sequeue *sq)
{
return (sq->front == (sq->rear+1)%N);
}
void EnSequeue(sequeue *sq, datatype x)
{
sq->rear = (sq->rear +1)%N;
sq->data[sq->rear] = x;
return;
}
datatype DeSequeue(sequeue *sq)
{
sq->front = (sq->front+1) % N;
return sq->data[sq->front];
}
void ClearSequeue(sequeue *sq)
{
sq->front = sq->rear = 0;
}
void PopBallStack(sqstack *s, sequeue *sq)
{
while(!EmptySqstack(s))
EnSequeue(sq, PopSqstack(s));
return;
}
int CheckSequeue(sequeue *sq)
{
int i;
for(i = 1; i<N; i++)
{
if(sq->data[(sq->front+i)%N] > sq->data[(sq->front+i+1)%N])
return 0;
}
return 1;
}
int main()
{
sqstack *s1, *s5, *s60;
sequeue *sq;
int i;
int no;
int flag=0, count=0;
int hour=0;
s1 = CreateSqstack();
s5 = CreateSqstack();
s60 = CreateSqstack();
sq = CreateSequeue();
for(i=1; i<N; i++)
EnSequeue(sq, i);
while(!flag)
{
count++;
no = DeSequeue(sq);
if(s1->top != M_1-1)
{
PushSqstack(s1, no);
}
else
{
PopBallStack(s1, sq);
if(s5->top != M_5-1)
{
PushSqstack(s5, no);
}
else
{
PopBallStack(s5, sq);
if(s60->top != M_60-1)
{
PushSqstack(s60, no);
}
else
{
PopBallStack(s60, sq);
EnSequeue(sq, no);
if(CheckSequeue(sq))
{
flag = 1;
printf("It passed %d minutes\n", count);
}
}
}
}
}
hour = count / 60;
printf("it passed %d days %d hours %d minutes\n", hour/24, hour%24, count%60);
return 0;
}
球钟问题
最新推荐文章于 2023-02-10 20:10:00 发布