士兵队列训练问题
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2567 Accepted Submission(s): 1160
2 20 40
1 7 19 1 19 37
#include <iostream>
#include <queue>
using namespace std;
queue<int> s;
void cmp(int k)
{
int i=1;
while(s.front()!=0)
{
if(i%k!=0) //把留下来的方队尾
s.push(s.front());
s.pop();i++; //去掉队首
}s.pop();s.push(0); //把队首的0去掉,在队尾加上0
}
int main (void)
{
int t,n,i,j,k,l;
cin>>t;
while(t--&&cin>>n)
{
for(i=1;i<=n;i++)
s.push(i);s.push(0); //每次在屁股加上个0
i=1;
while(s.size()>4) //因为加上了0,所以长度小于等于4就退出
{
if(i%2==0)cmp(3); //选择数三下踢一个人还是数两下踢一个人
else cmp(2);
i++;
}i=0;
while(!s.empty()) //查看队列中是不是为空
{
if(s.front()>0) //因为队尾还有个0,这个是不用输出的但是为了清空队列,还是要pop他
{
if(i)cout<<" ";i=1;
cout<<s.front();
}
s.pop();
}cout<<endl;
}
return 0;
}