某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。
Input 本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。 Output 共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。
Sample Input 2 20 40 Output 1 7 19 1 19 37
思路
通过取模模拟报数,模为0时删除迭代器所指向的元素。
AC代码
#include <iostream>
#include <list>
#include <algorithm>
#define AUTHOR "HEX9CF"
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
list<int> L;
for (int j = 1; j <= n; j++)
{
L.push_back(j);
}
int t = 2;
while (L.size() > 3)
{
int cnt = 1;
for (list<int>::iterator it = L.begin(); it != L.end();)
{
if (cnt++ % t == 0)
{
it = L.erase(it);
}
else
{
it++;
}
}
t = (t == 2) ? 3 : 2;
}
for (list<int>::iterator it = L.begin(); it != L.end(); it++)
{
if (it != L.begin())
{
cout << " ";
}
cout << *it;
}
cout << endl;
}
return 0;
}