对于本题,题的难度不是很大,关键是我就是没有思路。首先说一下我个人的解决思路。
#include <stdio.h>
void cnt(int n,int m)
{
int arr[n] = { 0 };
int count = 0;
int now = 0;
int cnt = 1;
while (arr[m - 1] == 0)
{
if (arr[now] == 0)
{
count++;
}
if (count == 3)
{
arr[now] = cnt;
cnt++;
count = 0;
if (arr[m - 1] != 0)
{
printf("%d", arr[m - 1]);
}
}
now = (now + 1) % n;
}
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
cnt(n, m);
return 0;
}
#include <stdio.h>
void cnt(int n,int m)
{ //因为用的是数组,所以小明所在的m位置数组下标就是arr[m-1]
int arr[n] = { 0 };//创建一个n个人的数组,并初始化各个原始为0
int count = 0;//count就是报数的数字,可以简单理解为一个计数器
int now = 0;
int cnt = 1; //第一个退出圈子,就是1,第二个退出圈子就是2。也就是先后退出的
//次序数字表示
while (arr[m - 1] == 0) //小明所在的位置没有退出,也即是没有赋值
{
if (arr[now] == 0)
{
count++; //如果arr[now]!=0也就是被赋值,已经退出,这时不进行count++
}
if (count == 3) //报到3则退出
{
arr[now] = cnt; //将报送3的所在位置进行cnt(退出的次序)赋值
cnt++; //下一个退出的次序
count = 0; //退出之后count=0初始化,重新开始报数
if (arr[m - 1] != 0)
{
printf("%d", arr[m - 1]);//如果小明所在位置已经被cnt赋值(退圈),直接打印所在位置的值
} //也就是第几个退出的
}
now = (now + 1) % n;//防止数组越界访问,在相应范围内进行报数
}
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);//n个人,在m位置
cnt(n, m);
return 0;
}