当前位置:网站首页>Vector|hdu-4841 round table questions

Vector|hdu-4841 round table questions

2022-06-13 01:09:00 includeSteven

HDU 4841 Round table questions

Topic link :HDU 4841 Round table questions

The question

Around the round table 2n personal . among n Individuals are good people , in addition n Individuals are bad people . If you start counting from the first person , Count to m personal , The person shall be executed immediately ; Then start counting after the people who are executed , Count to the third m Personal execution …… In this way, people sitting around the round table are executed . How to arrange the seats of these good and bad people in advance , It can make people put to death n After the individual , The rest of the round table n Individuals are all good people .

Input

Multiple sets of data , Data input for each group : The number of good and bad people n(<=32767)、 step m(<=32767)

Output

For each set of data , Output 2n Capital letters ,‘G’ Show good people ,‘B’ It means bad people ,50 One letter in a line , White space characters are not allowed . There is a blank line between adjacent data .

Sample Input

2 3
2 4

Sample Output

GBBG

BGGB

Ideas :

Use vector simulation , The number is 0-2n-1, All are good people at first , Then count and delete the m personal , Until deleted n One person only ;
Because it's a ring , Every time I count to the m You can delete it personally , So judge the second m The individual needs to take the mold ;
Last vector The rest of us are good people , Then we use the double pointer method , Output the value according to the corresponding position

Be careful :

Attention should be paid to when outputting , There is a blank line between every two sets of data , At the same time, one line can store at most 50 Letters

AC Code : Use G++ Submit

#include <iostream>
#include <vector>
using namespace std;
int main(){
    
    int n,m;
    while(cin>>n>>m){
    
        vector<int> v;
        int pos=0;  // The location of the person to delete 
        for(int i=0;i<2*n;i++)  v.push_back(i); // The number is 0-2*n-1
        for(int i=0;i<n;i++){
    
            pos = (pos+m-1)%(v.size()); // Note that there v The size of is constantly changing , Because they are constantly deleting v The elements in 
            v.erase(v.begin()+pos);
        }
        // Double pointer ,j from 0-2*n-1 Traverse , Only when v[i] The number of is equal to j The description number is j Of has not been deleted , That is, the corresponding person is a good person 
        for(int i=0,j=0;j<2*n;j++){
    
            if(j%50==0 && j)    cout<<endl; // When a line is larger than 50 Line break , Be careful j==0 You can't wrap lines 
            if(i<v.size() && v[i]==j){
    
                i++;
                cout<<"G";
            }else   cout<<"B";
        }
        cout<<endl<<endl;   // Note that there is a blank line between the two rows of data , So you need to output two blank lines 
    }
    return 0;
}
原网站

版权声明
本文为[includeSteven]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280555542724.html