<span style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">Time Limit:</span><span id="timeLimit" style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">1000</span><span style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">MS </span><span style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">Memory Limit:</span><span id="memoryLimit" style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">32768</span><span style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">KB </span><span style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">64bit IO Format:</span><span id="_64IOFormat" style="color: rgb(34, 34, 34); font-size: 16px; text-align: center;">%I64d & %I64u</span>
Description
某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。
Input
本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。
Output
共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。
Sample Input
2 20 40
Sample Output
1 7 19 1 19 37
//利用数组将要删除的人置零,然后输出非零,有大神用直接看出规律了....
#include <iostream>
using namespace std;
int main()
{int n,m,i,f,sum;
cin>>n;
while(n--)
{ int a[5000]={0};
cin>>m;
for(i=1;i<=m;i++)
a[i]=i;
sum=m;
if(m<=3)
{cout << "1";
m--;
for(i = 2; m; i++)
{
if(a[i])
{
cout << " " << a[i];
m--;
}
}
cout << endl; continue;
}
while (sum)
{f=0;
for (i=1;i<=m;i++)
{if(a[i])
f++;
if(f==2)
{a[i]=0;
f=0;
sum--;
}
}
if(sum<=3)
break;
f=0;
for (i=1;i<=m;i++)
{if(a[i])
f++;
if(f==3)
{a[i]=0;
f=0;
sum--;
}
}
if(sum<=3)
break;
}
cout << "1";
sum--;
for(i = 2; sum; i++)
{
if(a[i])
{
cout << " " << a[i];
sum--;
}
}
cout << endl;
}
return 0;
}