当前位置:编程学习 > 网站相关 >>

球钟问题到解决代码

#include <stdio.h>
#include <stdlib.h>
 
#define N 28
#define M 16
 
#define M_1 4   //分钟
#define M_5 11  //5分钟
#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;
    //3个栈的创建
    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;
}

 

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,