| 61.182.212.* |
1楼 围着桌子座着N个人,从1编号到N,从1号开始报数,报到3的离开桌子,下一个人再从1开始报数,周而复始,直到桌子上剩下一个人为止,问最后一个人的编号是多少? 以下是我写的小程序,花了我3个小时,希望大家给我些建议,我现在刚开始学习C,也不知道将来能不能靠这个吃饭。谢谢了!!! 我的Email:haibo-try100@163.com #include "stdio.h" #include "malloc.h" struct MyNode{ int num; struct MyNode* link; }; void init_CircleList(struct MyNode *first,int n) { struct MyNode *temp; int i=0; temp=first; for(i=1;i<n;i++) { temp->num=i; temp->link=(struct MyNode*)malloc(sizeof(struct MyNode)); temp=temp->link; } temp->num=n; temp->link=first; } void CircleDelt(int n,struct MyNode *head) { struct MyNode *temp,*delenode; temp=head; while(temp->link->num !=n) { temp=temp->link; } delenode=temp->link; temp->link=temp->link->link; } void CircleLook(struct MyNode *head) { struct MyNode *temp; temp=head; printf("现在围着桌子座的人有:\n"); printf("%d",temp->num); temp=temp->link; while(temp !=head) { printf(",%d",temp->num); temp=temp->link; } putchar('\n'); } int Caculate() { struct MyNode head; struct MyNode *temp,*tp; int totalnum=0,i=0; printf("请输入围着桌子座的总人数:"); scanf("%d",&totalnum); init_CircleList(&head,totalnum); // CircleLook(&head); temp=&head; i=2; while(temp->link != temp) { for(;i<=3;i++) temp=temp->link; printf("删除的人的号码为:%d\n",temp->num); tp=temp; temp=temp->link; CircleDelt(tp->num,tp); // CircleLook(temp); i=2; } printf("最后剩下的人的号码为:%d\n",temp->num); getchar(); return 1; } int main() { int choice; while(1) { printf("1.Caculate the number lasted in the gived circle \n2.exit\n"); scanf("%d",&choice); switch (choice){ case 1: Caculate(); break; case 2: exit(0); } } return 1; } |
|
|
- 共有14篇贴子
|
2楼 |
|
|
|
|
3楼 main() { int a[N],i,j=0,k=0; for(i=0;i<N;i++) a[i]=i+1; while(j<N-1) { for(i=0;i<N;i++) { if(a[i]!=0) {k++;} if(k==3) {a[i]=0;j++;k=0;} } } for(i=0;i<N;i++) if(a[i]!=0) {printf("%d\n",a[i]);} } |
|
|
|
| 218.192.91.* |
4楼 是1到100排成一圈,从1开始,数到第7的out,再接着数,问最后剩下的数是什么. |
|
|
|
5楼 void main(void) {char arr[]="123456"; int i=0,temp=0; int len; while ( arr[1]!='\0')//当围着桌子的人大于一个人的时候继续循环! { len=strlen(arr);//记录每次桌子剩下的人数! temp+=2;//每次向前数两个人! i=temp; while ( i > len-1 ) { i-=len; }//如果数组下标越界,自动循环回去!len-1为数组最大下标! temp=i;//记录当前的下标! while ( arr[i]!='\0' ) { arr[i]=arr[i+1]; i++; }//将要删除的人的代号,从数组中去掉! for ( i=0; i<strlen(arr); i++ ) { printf("%c ",arr[i]); }//输出每次剩下的人的代号! printf("\n-------------------------------\n"); } printf("\n%c\n",arr[0]);//输出最后剩下的人! getch(); } |
|
|
|
|
6楼 main() { int n, d, i, j, k, f, l, a[200]; l=1; printf("The number N:"); scanf("%d",&n); printf("The number D:"); scanf("%d",&d); for(i=1;i<=n;i++) a[i]=i; for(f=1;f<n;f++) { for(k=1,j=d;k<=j;k++,l++) { if(a[l]==0) j++; if(k==j) a[l]=0; if(l==n) l=0; if(k==j&&l==n) l=1; } } for(i=1;i<=n;i++) { if(a[i]!=0) printf("The last number is:%d\n\n",a[i]); } return 0; } |
|
|
|
|
7楼 |
|
|
|
|
8楼 /*----------------------------------------------- 围着桌子座着N个人,从1编号到N,从1号开始报数, 报到3的离开桌子,下一个人再从1开始报数,周而 复始,直到桌子上剩下一个人为止,问最后一个人 的编号是多少? -------------------------------------------------*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> int main() { int T,N,I,ONCE,LEN; /*LEN表示每次报数的长度*/ char *Tag,*P; puts("Enter Two Integer:"); scanf("%d,%d",&N,&LEN); Tag=(char*)malloc((N+1)*sizeof(char)); /*申请并初始化标识数组*/ for(I=0;I<N;I++) Tag[I]='0'; Tag[I]='\0'; T=N-1; I=0; /*开始报数*/ while(T) { ONCE=0; while(1) { if(Tag[I]=='0') ONCE++; if(ONCE==LEN) { Tag[I]='1'; break; } else if(I+1>N) I=0; else I++; } T--; } for(I=0;I<N;I++) if(Tag[I]=='0') break; printf("Last Number:%d\n",I+1); getch(); return 0; } |
|
|
|
|
9楼 #define N 13 #define L 3 main() { int i,k,m,a[N-1],*p; p=a; for(i=0;i<=N-1;i++)*(p+i)=i+1; i=0;k=0;m=0; while(m!=N-1) { if(*(p+i)!=0)k++; if(k==3){*(p+i)=0;k=0;m++} i++; if(i==N)i=0; } while(*p==0)p++; printf("the left is %d\n",*p); } |
|
|
|
| 61.131.139.* |
11楼 |
|
|
| 218.28.40.* |
12楼 |
|
|
| 221.0.182.* |
14楼 |
|
|
| 58.213.133.* |
15楼 #include<iostream> using namespace std; int main() { int n,count=0,i,a[20],exit=0; cout<<"几个人参加游戏?"; cin>>n; for(i=1;i<=n;i++) a[i]=i; for(i=1;exit!=n-1;i++) { if(a[i]==0) { if(i==n) i=0; continue; } count++; if(count==3) { count=0; a[i]=0; exit++; } if(i==n) i=0; } for(i=1;!a[i];i++); cout<<"最后留下的是第"<<i<<"个人\n"; return 0; } |
|
|
