web123456

Title: There are n people in a circle and numbers in sequence. Start counting from the first person (reporting from 1 to 3), and those who report to 3 leave the circle and ask the last person who is left behind. (simulation)

topic:
There are n people in a circle and numbers in sequence. Start counting from the first person (reporting from 1 to 3), and those who report to 3 leave the circle and ask the last person who is left behind.

Ideas:
The title says it is arranged in a circle, which can be seen as an arrangement of an array. Use i to traverse this array from the subscript of 1 (personal habits start from the subscript of 1). Whenever i==n+1, reset it to 1, so that n people are surrounded by a circle. Then, each element of the array is set to 1 at the beginning, indicating that it is in the circle. If you want to exit the circle, set it to 0.

Code:

#include <>
int main()
{
	int a[50], i, n, count, flag=0;
	scanf("%d", &n);
	count=n;
	for (i=1; i<=n; i++)
		a[i]=1;//Everyone is in the circle, for 1
	for (i=1; ;i++){
		if (i==n+1)
			i=1;//If i exceeds n person, let him return to 1
		
		if (a[i]!=0)	flag++;
		else	continue;
		
		if (flag%3==0){
			a[i]=0;
			count--;
		}
		
		if (count==1)// means that there is only one person left in the circle
			break;
	}
	for (i=1; i<=n; i++)
		if (a[i]!=0)
			printf("%d", i);//Note that here is the output i. The a[i] I wrote at the beginning, and I debugged it for a long time.
	return 0;
}