这是博主很久之前学C语言的时候遇到的一个有关算法的问题,当时作为小白的博主做了很久才解决。
题目的大概意思是这样的:
有n个人,从1开始数数,数到三出局,问剩下那个人是谁
初见这道题的时候还是一头雾水的,但通过画图分析可以得出解决问题的思路
下面是问题解决代码
```c
#include <stdio.h>
int main()
{
int num;
int other=0; //The preservation eliminated several people.
int i;
int count=0; //Used to count
int n; //Used to keep count of how many outs
printf("Enter an integer from the keyboard to indicate how many people are out of the game!\n");
scanf("%d",&num);
printf("Enter an integer from the keyboard to indicate several outs!\n");
scanf("%d",&n);
//Define an array to store these n people
int person[num];
for(i=0; i<num; i++) //Number n people 1-n
person[i]=i+1;
i=0;
while(other!=num-1)
{
if(person[i]!=0) //Key point: the deleted person is set to zero
count++; //3
if(count==n)
{
person[i]=0; //On the count of three, clear
count=0; //Restore count again
other++; //Statistics have eliminated several people at present.
}
i++; //5
if(i>num-1) //After counting, count again.
i=0;
}
//Print array,As long as it is 0--》eliminate not 0--》The rest
for(i=0; i<num; i++)
{
if(person[i]!=0)
printf("%d个人数%d出局,剩下的是第%d个人!\n",num,n,person[i]);
}
}
转载请声明出处:https://blog.csdn.net/xl575864781/article/details/104855180