Sicily 1931. 卡片游戏 (队列)

题目:http://soj.me/1931

一叠编号为1~n的牌。当至少还剩两张牌时进行以下操作:把第一张牌扔掉,然后把新的第一张放到整叠牌的最后。输入n,输出每次扔掉的牌,以及最后剩下的牌。 

思路:简单队列操作 模拟法

#include <iostream>
#include <cstdlib>
#include <queue>

using namespace std;

int arr[105];

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        queue<int> q;
        int n;
        cin>>n;
        int total=n;
        for(int i=1;i<=n;i++)
        {
            q.push(i);
        }
        while(total)
        {
            cout<<q.front()<<" ";
            q.pop();
            total--;
            if(total==0) break;
            int temp;
            temp=q.front();
            q.pop();
            q.push(temp);
        }
        cout<<endl;
    }
    return 0;
}

 

posted @ 2013-01-25 14:31  Daniel Qiu  阅读(231)  评论(0编辑  收藏  举报