问题; 一个经常出现的问题: n个人围成一圈,从第一个人开始1,2,3报数,当一个人报到数3时,退出.接下的从一在开始往下抱,遇3退出,依次循环下去,直到只剩一个人,输出这个人的原始数,如:输入7 结果输出;4 #include<stdio.h> #include<alloc.h> #include<conio.h> typedef struct node { int data; struct node *next; }node,*cllist; cllist head; void createlist(void) { cllist p,new,new1; int i; printf("please input the number of people:"); scanf("%d",&i); printf("\n"); while(i<=1) { printf("error due to the wrong input ! please input it again!\n"); scanf("%d",&i); } head=NULL; p=new=(node *)malloc(sizeof(node)); if(!new) { printf("overflow!"); exit(0); } new->next=head; head=new; head->data=i; while(i>1) { new1=(node *)malloc(sizeof(node)); if(!new1) { printf("overflow.\n"); exit(0); } i--; new1->next=head; head=new1; head->data=i; } p->next=head; p=p->next; while(head->next!=head) { p=head=head->next; p=p->next; head->next=head->next->next; free(p); if(head->next!=head) p=head=head->next; } printf("the left one's number is:%d",head->data); } main() { clrscr(); createlist(); }

评论