题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4841
2 3 2 4
GBBG BGGB
代码如下:
//用vector模拟约瑟夫环 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> using namespace std; int flag[50017]; vector<int> v; int main() { int n, m; int tot, now; int i; while(~scanf("%d%d",&n,&m)) { v.clear(); tot=2*n; for(i = 1; i <= tot; i++) { v.push_back(i); flag[i]=0; } now=1; /*for(i = 0; i < v.size(); i++) { printf("%d:%d\n",i,v[i]); }*/ while( tot > n )//只寻找坏人 { now+=(m-1); if(now <= tot) { flag[v[now-1]]=1;//从0开始计算 //printf(">%d<\n",v[now-1]); //printf("1: %d\n",*(v.begin()+now-1)); v.erase(v.begin()+now-1);//删除已经被flag[]标记的 now = (now==tot?1:now); } else { now%=tot; now = (now==0?tot:now); flag[v[now-1]]=1; //printf(">>%d<<\n",v[now-1]); //printf("2: %d\n",*(v.begin()+now-1)); v.erase(v.begin()+now-1);//删除已经被flag[]标记的 now = (now==tot?1:now); } tot--;//总数减一 } for(i = 1; i <= 2*n; i++) { if(flag[i]) printf("B"); else printf("G"); if(i%50==0) printf("\n"); } if((2*n)%50!=0) printf("\n"); printf("\n"); } return 0; }
hdu 4841 圆桌问题(用vector模拟约瑟夫环),布布扣,bubuko.com
原文地址:http://blog.csdn.net/u012860063/article/details/38299849