有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位?

#include<stdio.h>

int main()
{
	int n, i, temp, count = 0;
	printf("输入人数:\n");
	scanf("%d", &n);
	int a[n];
	temp = n;
	for(i = 0; i < temp; i++)
	{
		a[i] = i+1;
	}
	i = 0;
	while(temp > 1)
	{
		if(a[i] != 0)
		{
			count++;
		}
		if(count == 3)
		{
			count = 0;
			a[i] = 0;//置0表示淘汰
			temp--;
		}
		i++;
		if(i == n)
		{
			i = 0;
		}	
	}
	for(i = 0; i < n; i++)
	{
		if(a[i] != 0)
		{
			printf("The last one:%d\n", a[i]);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/costeeer/article/details/79165764
今日推荐