手拉手报数游戏

 对于本题,题的难度不是很大,关键是我就是没有思路。首先说一下我个人的解决思路。

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值