标签:style blog http color io os ar strong for
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 bool a[101]={0}; 6 intn,m,i,f=0,t=0,s=0; 7 cin>>n>>m; 8 do 9 { 10 ++t;//逐个枚举圈中的所有位置 11 if(t>n) 12 t=1;//数组模拟环状,最后一个与第一个相连 13 if(!a[t]) 14 s++;//第t个位置上有人则报数 15 if(s==m)//当前报的数是m 16 { 17 s=0;//计数器清零 18 cout<<t<<‘‘;//输出被杀人编号 19 a[t]=1;//此处人已死,设置为空 20 f++;//死亡人数+1 21 } 22 }while(f!=n);//直到所有人都被杀死为止 23 return 0; 24 }
C代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct node 5 { 6 int data; 7 struct node *next; 8 }Node; 9 10 Node *creat(int n) 11 { 12 Node *p, *head; 13 head = (Node *)malloc(sizeof(Node)); 14 p = head; 15 Node *s; 16 int i = 0; 17 18 if(0 != n) 19 { 20 while(i <= n) 21 { 22 s = (Node *)malloc(sizeof(Node)); 23 s->data = i++; 24 p->next = s; 25 p = s; 26 } 27 s->next = head->next; 28 } 29 free(head); 30 return s->next; 31 } 32 33 int main() 34 { 35 int n = 41, m = 3; 36 int i; 37 Node *p = creat(n); 38 Node *temp; 39 40 m %= n; 41 42 while(p != p->next) 43 { 44 for(i = 1; i < m-1; i++) 45 { 46 p = p->next; 47 } 48 printf("%d ", p->next->data); 49 temp = p->next; 50 p->next = temp->next; 51 52 free(temp); 53 p = p->next; 54 } 55 printf("%d\n", p->data); 56 57 return 0; 58 }
标签:style blog http color io os ar strong for
原文地址:http://www.cnblogs.com/shiddong/p/3976009.html