当前位置:网站首页>The problem of Joseph in Informatics

The problem of Joseph in Informatics

2022-06-12 13:23:00 Xiao Liu wants to be number one

2037:【 example 5.4】 Joseph's question


The time limit : 1000 ms         Memory limit : 65536 KB
Submission number : 10954     Passing number : 5588

【 Title Description 】

NN A circle of individuals , Count from the first person , Count to MM People out of circles ; Then the next person starts counting , Count to MM People out of circles ;… Output the number of people who circle in turn .

【 Input 】

Input NN and MM.

【 Output 】

Output one line , Number the people in the circle in turn .

【 sample input 】

8 5

【 sample output 】

5 2 8 7 1 4 6 3

【 Tips 】

【 Data range 】

For all the data ,2≤N,M≤10002≤N,M≤1000.

#include <iostream>
using namespace std;
int main()
{
	int m, n, a[1000], i;
	cin >> n >> m;
	for (i = 0; i < n; i++)
		a[i] = i + 1;// Everyone's serial number 
	int x = 0, y = 0;//x Is the number of people kicked ,y That's the number 
	i = 0;
	while (x < n)
	{
		if (a[i] != 0)
			y++;
		if (y == m)
		{
			a[i] = 0;// After being kicked out, change its serial number to 0
			y = 0;
			x++;
			cout << i + 1 << " ";
		}
		i++;
		if (i == n)i = 0;// Can't write here break, If break, You can only cycle once 
	}
return 0;
}

原网站

版权声明
本文为[Xiao Liu wants to be number one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010517130435.html