山再高,往上爬,總能登頂;
路再長,走下去,定能到達。
圓桌問題
題目描述
圓桌上圍坐着2n個人其中n個人是好人,另外n個人是壞人。如果從第一個人開始數數數到第m個人,則立即處死該人,然後從被處死的人之後開始數數,再將數到的第m個人處死…… 依此方法不斷處死圍坐在圓桌上的人。試問預先應如何安排這些好人與壞人的座位,能使得在處死n個人之後,圓桌上圍坐的剩餘的n個人全是好人。
輸入
僅一行都有兩個數依次爲n和m,表示一個問題的描述信息。
輸出
輸出問題的解,問題的解可以用連續的若干行字符來表示,每行的字符數量不超過50,但是在問題的解中不允許出現空白字符和空行,用大寫字母 G 表示好人、大寫字母 B 表示壞人。
Sample Input
2 3
Sample Output
GBBG
題目大意
這題的意思是經典的約瑟夫環問題,每逢數字n就被槍決。
然後有幾個人是串通好的,他們不想被殺掉。所以他們應該留在最後。
思路分析
這題主要就是一個模擬思路,遇到就剔除就好。
我的思路就是加一個標記,超過限制就取餘,遇到就eraser
實際上玩的就是一個vector操作
然後就是輸出了,每50個換一個行
AC時間到
#include<algorithm>
#include<iostream>
#include<string.h>
#include <iomanip>
#include<stdio.h>
#include<utility>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
#pragma warning(disable:4244)
#define PI 3.141592653589793
#pragma GCC optimize(2)
#define accelerate cin.tie(NULL);cout.tie(NULL);ios::sync_with_stdio(false);
#define EPS 1.0e-8
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const char char_inf = 127;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll read() {
ll c = getchar(), Nig = 1, x = 0;
while (!isdigit(c) && c != '-')c = getchar();
if (c == '-')Nig = -1, c = getchar();
while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
return Nig * x;
}
inline void out(ll a) {
if (a < 0)putchar('-'), a = -a;
if (a >= 10)out(a / 10);
putchar(a % 10 + '0');
}
ll phi(ll n)
{
ll ans = n, mark = n;
for (ll i = 2; i * i <= mark; i++)
if (n % i == 0) { ans = ans * (i - 1) / i; while (n % i == 0)n /= i; }
if (n > 1)ans = ans * (n - 1) / n; return ans;
}
ll qpow(ll x, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res;
}
#define Floyd for(int k = 1; k <= n; k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
#define read read()
vector<int>q;
bool judge[100000];
int main()
{
int n = read, m = read;
int mark = n << 1;
for (int i = 0; i < mark; i++)//存入編號從0開始就行
q.push_back(i);//從0開始標記也一樣,只不過取餘操作需要改變下
int id = 0;//位置標記
for (int i = 0; i < n; i++)
{
id = (id + m - 1) % q.size();//超過邊界返回第一個人
q.erase(q.begin() + id);
}
int N = 0;
for (auto i : q)judge[i] = 1;//i是迭代q裏所有剩餘的元素
for (int i = 0; i < mark; i++)
{
if (judge[i])putchar('G');//如果活到最後
else putchar('B');//如果沒有活到最後
N++;//50次換行
if (N == 50)
{
N = 0;
putchar('\n');
}
}
}
By-輪月