士兵隊列訓練問題
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3441 Accepted Submission(s): 1595
1 7 19 1 19 37
這個是用STL裏的隊列做的,不懂的可以瞭解一下STL,學會了這個可以直接拿來運用。
代碼:
#include<stdio.h>
#include<queue>
using namespace std ;
queue<int> q ;
void remove(int x)
{
int i = 1 ;
while(q.front() != 0)
{
if(i%x != 0)//留下來的隊伍
q.push(q.front()) ;
q.pop();//去掉隊首
i++ ;
}
q.pop();
q.push(0) ;//把隊尾的0去掉再在隊尾加上0
}
int main()
{
int N = 0 ;
scanf("%d" , &N) ;
while(N--)
{
int n = 0 ;
scanf("%d",&n);
int i = 0 ,j = 0 ;
for(i = 1 ; i <= n ; i++ )
{
q.push(i) ;
}
q.push(0) ;
int k = 1 ;
while(q.size() > 4)//因爲加上了0,所以長度小於4時就退出
{
if(k % 2)
remove(2) ;//如過變換的次數爲奇數則數1~2,踢掉2,否則數1~3,踢掉3
else
remove(3) ;
k++;
}
j = 0 ;
while(!q.empty())
{
if(q.front() > 0)
{
if(j)
printf(" ");
j = 1 ;
printf("%d", q.front());
}
q.pop() ;
}
printf("\n") ;
}
return 0 ;
}