Round Table Issues
Time limit MS Memory limit 32768 KB OS Windows
problem Description
The round table was surrounded by 2n individuals. where n is a good person, and the other n is a bad person. If the number of persons starting from the first person is counted, and the number of persons to m, the person shall be executed immediately, and then the number of persons executed shall be counted, and then the number of individual m will be executed. In this way, people who sit around the table are continually executed. How to arrange the seats of these good and bad people beforehand, so that after the execution of the n individuals, the remaining n individuals sitting around the table are all good people. Input
Multiple sets of data, input per group: number of good and bad People N (<=32767), Step m (<=32767); Output
For each set of data, output 2n uppercase letters, ' G ' for good people, ' B ' for bad guys, 50 letters for one line, no whitespace characters allowed. A blank line is left between adjacent data. Sample Input
2 3
2 4 Sample Output
Gbbg
BGGB Problem Solving ideas:
Used to be recursive, this time because of learning STL, it is specially used vector to do. The core code is
no= (no+m-1)%v.size ();
vis[v[no]]=b;
V.erase (V.begin () +no);
Use vector v to store the person's number, the VIS array tag the person's good or bad, each find the bad guy, record number, and delete this element in V
output a bit of a pit, it is not the data between the blank line, but directly after each output after a blank line on the line
Code:
#include <iostream> #include <string> #include <vector> #include <
Cstring> using namespace std;
const int m=32767+5;
const int g=0,b=1;
int vis[2*m];
int main () {int n,m,first=1;
while (cin>>n>>m) {memset (vis,g,sizeof (VIS));
Vector<int> V (2*n);
for (int i=0;i<2*n;i++) v[i]=i;
int no=0;
for (int i=0;i<n;i++) {no= (no+m-1)%v.size ();
Vis[v[no]]=b;
V.erase (V.begin () +no);
} for (int i=0;i<2*n;i++) {if (i%50==0&&i>0) cout<<endl;
if (vis[i]==g) cout<< ' G ';
else cout<< ' B ';
} cout<<endl<<endl;
} return 0; }