uva 10935 Throwing cards away I 习题5-3 (STL queue练习)

题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=21&page=show_problem&problem=1876


说明:是入门竞赛第一版第五单元的例题;用queue很容易做出来。


#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
int main(){
    int n;
    while(cin >> n&&n){
        queue<int> q;
        for(int i=1;i<=n;i++)
            q.push(i);

        int flag=1;
        cout<<"Discarded cards:";
        while(q.size()!=1){
            if(flag) flag=0;else cout<<",";
            cout << " "<<q.front();
            q.pop();
            q.push(q.front());
            q.pop();
        }
        cout<<endl<<"Remaining card: "<<q.front()<<endl;
        q.pop();
    }
    return 0;
}


你可能感兴趣的:(uva 10935 Throwing cards away I 习题5-3 (STL queue练习))