关于排序问题

来源:互联网 发布:wireshark过滤端口抓包 编辑:程序博客网 时间:2024/04/18 12:42

2个小朋友手拉手站成一个圆圈,从第一个小朋友开始报数,报到6的那个小朋友退出到圈外,然后他的下一位重新报“1”。这样继续下去,最后只剩下一个小朋友,他原来站在什么位置上呢? 并把输出小朋友退出圈外的顺序。

#include <stdio.h>


void main()
{
int a[12]={0},b[12]={0};  
// a is for children, if a[x]==1 then x is still in the circle. 
// b is used to record the get out order.
int count,i,j;

for(i=0;i<12;i++)
{
a[i]=1; 
}


i=0;j=0;count=0;
  while(j<12)
{
count=count+a[i];
if(count==6)
{
b[j++]=i+1;
count=0;
a[i]=0;
}
  i++;
  if(12==i) 
  i=0;
}

printf("The last person in the circle is : %d\n",b[11]);
printf("The order is: ");
for(i=0;i<12;i++)
printf("%d  ",b[i]);
printf("\n");
}

0 0