问题描述:
球钟是一个利用球的移动来记录时间的简单装置。它有三个可以容纳若干个球的指示器:
分钟指示器,五分钟指示器,小时指示器。若分钟指示器中有2个球,五分钟指示器中有
6个球,小时指示器中有5个球,则时间为5:32
工作原理:每过一分钟,球钟就会从球队列的队首取出一个球放入分钟指示器,分钟指示
器最多可容纳4个球。当放入第五个球时,在分钟指示器的4个球就会按照他们被放入时
的相反顺序加入球队列的队尾。而第五个球就会进入五分钟指示器。按此类推,五分钟
指示器最多可放11个球,小时指示器最多可放11个球。当小时指示器放入第12个球时,
原来的11个球按照他们被放入时的相反顺序加入球队列的队尾,然后第12个球也
回到队尾。这时,三个指示器均为空,回到初始状态,从而形成一个循环。因此
,该球钟表示时间的范围是从0:00到11:59。
要求:现设初始时球队列的球数为27,球钟的三个指示器初态均为空。问,
要经过多久,球队列才能回复到原来的顺序?
main.c函数
#include "SqStack.h"
#include "linkqueue.h"
int eq(linkqueue_t *q,linkqueue_t *p);
int main(int argc, char const *argv[])
{
linkqueue_t *q,*th;
q = LinkQueueCreate();
th = LinkQueueCreate();
for(int i = 1; i<=27; i++) {
LinkQueueEnQueue(q,i);
}
for(int i = 1; i<=27; i++) {
LinkQueueEnQueue(th,i);
}
seqstack_t *_5min_, *_1min_, *_hour_;
_1min_ = SeqStackCreat();
_5min_ = SeqStackCreat();
_hour_ = SeqStackCreat();
int count = 0;
int a = 1;
int day = 0;
int n = 5;
while (a || eq(th,q))
{
a = 0;
for(int i = 0 ;i< 12;i++) {
for(int j = 0; j < 12 ;j++) {
for (int k = 1; k < 5;k++) {
SeqStackPush(_1min_,LinkQueueDeQueue(q));
count ++;
}
for (int k = 1; k < 5;k++) {
LinkQueueEnQueue(q,SQStachDeleteData(_1min_));
}
if(j == 11)
continue;
count ++;
SeqStackPush(_5min_,LinkQueueDeQueue(q));
}
for (int j = 1; j < 12;j++) {
LinkQueueEnQueue(q,SQStachDeleteData(_5min_));
}
if(i == 11)
continue;
count++;
SeqStackPush(_hour_,LinkQueueDeQueue(q));
}
for (int i = 1; i < 12;i++) {
LinkQueueEnQueue(q,SQStachDeleteData(_hour_));
}
day ++;
count++;
SeqStackPush(_1min_,LinkQueueDeQueue(q));
LinkQueueEnQueue(q,SQStachDeleteData(_1min_));
}
LinkQueueShow(q);
LinkQueueShow(th);
printf("%d天\n",day/2);
printf("%dmin\n",count);
return 0;
}
int eq(linkqueue_t *q,linkqueue_t *p)
{
node_t *th1,*th2;
th2 = p->front;
th1 = q->front;
while (th1->next)
{
if(th1->next->data != th2->next->data) {
return 1;
}
th1 = th1->next;
th2 = th2->next;
}
return 0;
}
linkqueue.h
#ifndef __LINKQUEUE_H__
#define __LINKQUEUE_H__
#include <stdio.h>
#include <stdlib.h>
#define datatype int
typedef struct node1
{
datatype data;
struct node1 *next;
}node_t;
typedef struct
{
node_t *front,*rear;
}linkqueue_t;
linkqueue_t* LinkQueueCreate(void);
int LinkQueueEnQueue(linkqueue_t* q, datatype data);
int LinkQueueIsEmpty(linkqueue_t* q);
datatype LinkQueueDeQueue(linkqueue_t* q);
void LinkQueueShow(linkqueue_t* q);
#endif
linkqueue.c
#include "linkqueue.h"
linkqueue_t* LinkQueueCreate(void)
{
node_t *h;
h = (node_t *)malloc(sizeof(*h));
if(h == NULL) {
printf("%s node malloc fail\n",__func__);
return NULL;
}
h->data = 0;
h->next = NULL;
linkqueue_t *q;
q = (linkqueue_t *)malloc(sizeof(*q));
if(q == NULL) {
printf("%s linkqueue malloc fail\n",__func__);
return NULL;
}
q->front = h;
q->rear = h;
return q;
}
int LinkQueueEnQueue(linkqueue_t* q, datatype data)
{
node_t *temp;
temp = (node_t *)malloc(sizeof(*temp));
if(temp == NULL) {
printf("%s malloc fail\n",__func__);
return -1;
}
temp->data = data;
temp->next = NULL;
q->rear->next = temp;
q->rear = temp;
return 0;
}
int LinkQueueIsEmpty(linkqueue_t* q)
{
return (q->front) == (q->rear);
}
datatype LinkQueueDeQueue(linkqueue_t* q)
{
if(LinkQueueIsEmpty(q)) {
printf("%s q is empty\n",__func__);
return 0;
}
node_t *temp;
datatype data;
temp = q->front->next;
if(temp->next == NULL)
q->rear = q->front;
data = temp->data;
q->front->next = temp->next;
free(temp);
temp = NULL;
return data;
}
void LinkQueueShow(linkqueue_t* q) {
node_t *th;
th = q->front;
while (th->next )
{
printf("-%d",th->next->data);
th = th->next;
}
printf("-\n");
}
SqStack.h
#ifndef __SQSTACK_H__
#define _SQSTACK_H__
#include <stdio.h>
#include <stdlib.h>
#define datatype int
#define MAXSIZE 30
typedef struct node
{
int top;
datatype data[MAXSIZE];
}seqstack_t;
seqstack_t* SeqStackCreat(void);
int SeqStackIsFull(seqstack_t *h);
int SeqStackPush(seqstack_t *h,datatype data);
int SeqStackIsEmpty(seqstack_t *h);
datatype SQStachDeleteData(seqstack_t *h);
#endif
SqStack.c
#include "SqStack.h"
seqstack_t* SeqStackCreat(void)
{
seqstack_t *h;
h = (seqstack_t *)malloc(sizeof(*h));
if(h == NULL) {
printf("%s malloc is fail \n",__func__);
return NULL;
}
h->top = -1;
return h;
}
int SeqStackIsFull(seqstack_t *h)
{
return h->top == MAXSIZE - 1;
}
int SeqStackPush(seqstack_t *h,datatype data)
{
if(SeqStackIsFull(h)) {
printf(" %s sqstack is full\n",__func__);
return -1;
}
h->top++;
h->data[h->top] = data;
return 0;
}
int SeqStackIsEmpty(seqstack_t *h)
{
return h->top == -1;
}
datatype SQStachDeleteData(seqstack_t *h)
{
if(SeqStackIsEmpty(h)) {
printf(" %s sqstack is empty\n",__func__);
return -1;
}
return h->data[h->top--] ;
}
运行截图: