[linked list topic] soldier queue training


A unit carries out a queue training of recruits. The recruits are numbered in sequence from the beginning and lined up in a horizontal line. The training rules are as follows: report from the beginning one to two. For those who report to the second column, the rest move closer to the small sequence number, and then report from the beginning one to three. For those who report to the third column, the rest move closer to the small sequence number, Continue counting from one to two from the beginning…, After that, take turns to report one to two and one to three from the beginning until the remaining number does not exceed three.
This question has multiple test data groups. The number of the first behavior group is n, followed by the number of recruits in line n, and the number of recruits does not exceed 5000.
There are n lines in total, corresponding to the number of recruits entered respectively. Each line outputs the initial number of the remaining recruits, with a space between the numbers.
Sample Input
Sample Output
1 7 19
1 19 37

using namespace std;
//#Include < bits / STDC + +. H > // Universal header file 

int main(){
    int t = 0, n = 0;
    cin >> t;
    While (T --) {// number of test groups 
        cin >> n;// Number of soldiers 
        int k = 2;// Delete the specified number of intervals, initially 2, 2 and 3 alternately 
        list<int >blist;// Create an empty linked list 
        list<int >::iterator it;// Create a list iterator
        for(int i = 1;  i <= n;  I + +) {// assign an initial value to the linked list from position 1 
        While (blist. Size() > 3) {// keep the last 3 elements 
            int num;// Counting man 
            num = 0;
            for(it = blist.begin(); it !=  blist.end();){// Because the value corresponding to the linked list should be deleted directly after finding it and traversed with an iterator 
//                if(num++ % k == 0){//
                If (Num% k = = 0) {// this writing method can't get the correct answer 
                    it = blist.erase(it);// Delete the specified element that meets the condition
                    it++;// If it cannot be deleted, it needs to increase automatically, if it is not an endless cycle 
            If (k = = 2) {// complete the 2,3 conversion 
                k = 3;
                k = 2;
        for(it = blist.begin();  it !=  blist.end();  It + +) {// traverse the remaining three numbers according to the format 
            if(it != blist.begin()){
                cout << " ";
            cout << *it;
        cout << endl;
    return 0;

[linked list topic] soldier queue training

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]