hdu 1276 士兵队列训练问题

士兵队列训练问题

 思路:这道题是属于容器的题,当我们读到题时,我们先想一下,这个适合用什么。我们可以直接读到,一直以1~2或1~3报数的方式,删除容器中的元素,此时我们就可以确定,我们使用list更加方便。此时,大框架已经基本完成,我们只需要模拟整个过程即可。

代码:

#include<iostream>
#include<list>

using namespace std;

int main(){

    int n, i, num, k, t;
    while (cin >> t){

        while (t--){
            k = 2;
            cin >> n;
            list<int> l;
            list<int>::iterator it;
            for (i = 1; i <= n; i++)
                l.push_back(i);
            while (l.size() > 3){
                num = 1;
                for (it = l.begin(); it != l.end();){
                    if (num++%k == 0)
                    {
                        it = l.erase(it);
                        
                    }
                    else{
                        it++;
                    }

                }
                if (k == 2){
                    k = 3;
                }
                else{
                    k = 2;
                }
            }
            for (it = l.begin(); it != l.end();it++){
                if (it != l.begin())
                    cout << " ";
                cout << *it;
            }
            cout << endl;

        }
    }
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/pcdl/p/12369278.html