其他分享
首页 > 其他分享> > hdu 4841 圆桌问题

hdu 4841 圆桌问题

作者:互联网

圆桌问题

(约瑟夫环的变性)

圆桌上围坐着2n个人。其中n个人是好人,另外n个人是坏人。如果从第一个人开始数数,数到第m个人,则立即处死该人;然后从被处死的人之后开始数数,再将数到的第m个人处死……依此方法不断处死围坐在圆桌上的人。试问预先应如何安排这些好人与坏人的座位,能使得在处死n个人之后,圆桌上围坐的剩余的n个人全是好人。

Input
多组数据,每组数据输入:好人和坏人的人数n(<=32767)、步长m(<=32767);

Output
对于每一组数据,输出2n个大写字母,‘G’表示好人,‘B’表示坏人,50个字母为一行,不允许出现空白字符。相邻数据间留有一空行。

Sample Input

2 3
2 4

Sample Output

GBBG
BGGB

代码:

#include<stdio.h>
#include<iostream>
#include<vector>
#include<cstring>

using namespace std;


int main()
{
	vector<int> table;
	int n, m;

	while (cin>>n>>m)
	{
		table.clear();
		for (int i = 0; i < 2*n; i++)
		{
			table.push_back(i);
		}
		int pos = 0; // 记录当前位置
		// 赶走n个人
		for (int i = 0; i < n; i++)
		{
			// 圆桌是个环,做取余处理
			pos = (pos + m - 1) % table.size();
			table.erase(table.begin() + pos);// 赶走换人,table中的人数-1
		}
		int j = 0;
		for (int i = 0; i < 2*n; i++)
		{
			if (!(i%50)&&i)					// 五十个字 为一行
				cout << endl;
			if (j < table.size() && i == table[j])	// table留下的都是好人
			{
				j++;
				cout << "G";
			}
			else
				cout << "B";
		}
		cout << endl;

	}

	return 0;
}

标签:hdu,个人,int,pos,table,include,圆桌,4841
来源: https://blog.51cto.com/u_15265965/2892312