The fox catches the rabbit (c/c++ implementation)

Problem description
There are 10 circular holes arranged around the top of the mountain. The fox wants to eat the rabbit. The rabbit said: "Yes, but you must find me. I will hide in these 10 holes. You go to hole 1 first, and second Look for 1 hole every time (i.e. hole 3), and look for 2 holes (i.e. hole 6) for the second time, and so on in the future, and there is no limit to the number of times." But the fox went in and out 1,000 times from morning to night. The rabbit is still not found. Q: Where is the rabbit hiding in?

#include<iostream>
using namespace std;
typedef int Sqlist[100];
//函数声明
void Initlist(Sqlist L, int n);//初始化兔子洞
void capture(Sqlist L, int n, int m);//捕捉兔子函数
void judge(Sqlist L, int n);//判断兔子位置
int main()
{
    
    
	int cavesize, time;
	cout << "请输入你要设置__个洞口:";
	cin >> cavesize;
	cout << "请输入狐狸寻找的次数:";
	cin >> time;
	Sqlist L;
	Initlist(L, cavesize);
	capture(L, cavesize, time);
	judge(L, cavesize);
	return 0;
}

void Initlist(Sqlist L, int n)
{
    
    
	int i;
	for (i = 1; i <= n; i++)
	{
    
    
		L[i] = 1;
	}
}

void capture(Sqlist L, int n, int m)
{
    
    
	int carry = 1, num = 0, i;
	for (i = 0; i < m; i++)
	{
    
    
		num = num + carry;
		if (num == n)L[n] = 0;
		else L[num % n] = 0;
		carry++;
	}
}

void judge(Sqlist L, int n)
{
    
    
	cout << "兔子可能的藏身之处为:";
	int i;
	for (i = 1; i <= n; i++)
	{
    
    
		if (L[i] == 1)
		{
    
    
			cout << i << "号洞" << " ";
		}
		else
		{
    
    
		    cout<<"无"<<endl<<"狐狸逮到了兔子";
		    break;
		}
	}
	cout << endl;
}

(PS: If the rabbit hole to be set exceeds 100, you need to modify the code again)

Guess you like

Origin blog.csdn.net/gets_s/article/details/104996411