1996约瑟夫环

约瑟夫环

用队列的方法解题


```cpp
#include<iostream>
#include<queue>
using namespace std;
int main()
{
	int n, out,now=1;
	cin >> n >> out;
	queue<int> q;
	for (int i = 1; i <= n; i++)
		q.push(i);
	while (!q.empty())
	{
		if (now == out)
		{
			now = 1;//重新计数
			cout << q.front() << " ";
			q.pop();
		}
		else
		{
			int current = q.front();
			q.pop();
			q.push(current);
			now++;
		}
	}

	return 0;
}```

猜你喜欢

转载自blog.csdn.net/helloworld0529/article/details/102962610
今日推荐