圆圈中最后剩下的数字

12个小朋友手拉手站成一个圆圈,从第一个小朋友开始报数,报到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");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值