hdu4841 圆桌问题(数组模拟)

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

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

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

【输入样例】
2 3
2 4

【输出样例】
GBBG
BGGB

【算法代码】

#include <bits/stdc++.h>
using namespace std;
 
const int maxn=32767<<1+5;
int flag[maxn];
 
int main() {
	int n,m;
	while(cin>>n>>m) {
		for(int i=1; i<=2*n; i++) flag[i]=1;
		int tot=2*n;
		int cur=0;
		while(tot>n) {
			for(int i=1; i<=2*n; i++) {
				if(!flag[i]) continue;
				if(tot==n) break;
				cur++;
				if(cur==m) {
					flag[i]=0;
					cur=0;
					tot--;
				}
			}
		}
 
		for(int i=1; i<=2*n; i++) {
			if(!flag[i]) cout<<"B";
			else cout<<"G";
 
			if(i%50==0) cout<<endl;
		}
		cout<<endl<<endl;
	}
 
	return 0;
}
 
 
/*
input:
2 3
2 4
output:
GBBG
BGGB
*/


【参考文献】
https://blog.csdn.net/sterben_da/article/details/51171017
https://blog.csdn.net/ngq0273/article/details/97391019
https://blog.csdn.net/bjxqmy/article/details/98348663
https://blog.csdn.net/qq1013459920/article/details/83622880
 


版权声明:本文为hnjzsyjyj原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。