#include<stdio.h>
#include<stdlib.h>
typedef struct queue{
int data;
struct queue *next;
}QueueNode;
typedef struct{
QueueNode *rear,*front;
}LinQueue;
void InitiateQueue(LinQueue *Q)
{
Q->front=Q->rear=NULL;
}
int IsEmptyQueue(LinQueue *Q)
{
return Q->front==NULL;
}
void InQueue(LinQueue *Q,int item)
{
QueueNode *p;
p=(QueueNode *)malloc(sizeof(QueueNode));
p->data=item;
p->next=NULL;
if(Q->front==NULL)
Q->front=Q->rear=p;
else{
Q->rear->next=p;
Q->rear=p;
}
}
int OutQueue(LinQueue *Q)
{
QueueNode *p;
int x;
if(IsEmptyQueue(Q))
exit(1);
p=Q->front;
x=p->data;
if(Q->rear==p)
{free(p);
return x;}
else{
Q->front=p->next;
free(p);
return x;
}
}
int GetQueuTop(LinQueue *Q)
{
if(IsEmptyQueue(Q))
exit(1);
return Q->front->data;
}
typedef struct stacknode{
int data;
struct stacknode *next;
}Stacknode;
typedef struct{
Stacknode *top;
}LinStack;
void InitiateStack(LinStack *s){
s->top=NULL;
}
int IsEmptyStack(LinStack *s){
return s->top==NULL;
}
void push(LinStack *s,int item)
{
Stacknode *p;
p=(Stacknode *)malloc(sizeof(Stacknode));
p->data=item;
p->next=s->top;
s->top=p;
}
int pop(LinStack *s)
{
Stacknode *p;
int x;
if(IsEmptyStack(s))
exit(1);
p=s->top;
x=p->data;
s->top=p->next;
free(p);
return x;
}
int top(LinStack *s)
{
if(IsEmptyStack(s))
exit(1);
return s->top->data;
}
void main(){
LinQueue Q;
LinStack Ss,Sm,Sh;
int i,counts=0,countm=0,counth=0,counthalfday=1,b[45],flag=0,d[45];
InitiateStack(&Ss);
InitiateStack(&Sm);
InitiateStack(&Sh);
InitiateQueue(&Q);
for(i=1;i<=45;i++) //初始化,输入1-45
InQueue(&Q,i);
for(i=0;i<45;i++)
b[i]=i+1;
while(1){
while(countm<=11||counth==11){
while(counts<4||countm==11&&counth==11){
push(&Ss,OutQueue(&Q));
counts++;
if(counts==4&&countm==11&&counth==11)
{counts=0;
countm=0;
counth=0;
for(i=0;i<4;i++)
InQueue(&Q,pop(&Ss));
for(i=0;i<11;i++)
InQueue(&Q,pop(&Sm));
for(i=0;i<11;i++)
InQueue(&Q,pop(&Sh));
InQueue(&Q,OutQueue(&Q));
for(i=0;i<45;i++)
d[i]=OutQueue(&Q);
printf("排序数字:\n");
for(i=0;i<45;i++)
printf("%d\t",d[i]);
printf("\n\n");
InitiateQueue(&Q);
for(i=0;i<45;i++)
InQueue(&Q,d[i]);
i=0; //判断是否跟初始化的队列一样
while(i<45)
{
if(b[i]==d[i])
i++;
else break;
}
if(i==45)
flag=1;
if(flag==1)
break;
counthalfday++;
}
}
if(flag==1)
break;
counts=0;
for(i=0;i<4;i++)
InQueue(&Q,pop(&Ss));
if(IsEmptyStack(&Sm)&&IsEmptyStack(&Sh)){ //判断是否跟初始化的队列一样
for(i=0;i<45;i++)
d[i]=OutQueue(&Q);
for(i=0;i<45;i++)
printf("%d\t",d[i]);
printf("\n\n");
InitiateQueue(&Q);
for(i=0;i<45;i++)
InQueue(&Q,d[i]);
i=0;
while(i<45)
{
if(b[i]==d[i])
i++;
else break;
}
if(i==45)
flag=1;
if(flag==1)
break;
}
if(countm==11)
break;
push(&Sm,OutQueue(&Q));
countm++;
}
if(flag==1)
break;
countm=0;
for(i=0;i<11;i++)
InQueue(&Q,pop(&Sm));
if(IsEmptyStack(&Sm)&&IsEmptyStack(&Sh)){ //判断是否跟初始化的队列一样
for(i=0;i<45;i++)
d[i]=OutQueue(&Q);
for(i=0;i<45;i++)
printf("%d\t",d[i]);
printf("\n\n");
InitiateQueue(&Q);
for(i=0;i<45;i++)
InQueue(&Q,d[i]);
i=0;
while(i<45)
{
if(b[i]==d[i])
i++;
else break;
}
if(i==45)
flag=1;
if(flag==1)
break;
}
push(&Sh,OutQueue(&Q));
counth++;
}
printf("总共要%d天\n",counthalfday/2);
}
这个是输入45个球的。答案上机验证过了。绝对正确!!!!!
378
哈哈。通宵写完了~~~~~~~~大家慢慢看。经典~
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
[此贴子已经被作者于2005-4-30 8:52:35编辑过]