UVa 10935 - Throwing cards away I(模拟)

给出牌的顺序,输出每次扔掉的牌和最后剩下的牌。挺简单的道题,直接模拟就行。用的vector,一开始输出格式错了。改改就过了。

#include<iostream>
#include<vector>
using namespace std;
int n;
vector<int>cards;
int main()
{
    while(cin>>n&&n)
    {
        int first=1;
        for(int i=1;i<=n;i++)
            cards.push_back(i);
        cout<<"Discarded cards:";
        while(!cards.empty())
        {
            if(cards.size()==1)
                break;
            if(first)
            {
                first=0;
                cout<<" ";
            }
            else
                cout<<", ";
            cout<<cards[0];
            cards.erase(cards.begin());
            cards.push_back(cards[0]);
            cards.erase(cards.begin());
        }
        cout<<endl;
        cout<<"Remaining card: "<<cards[0]<<endl;
        cards.clear();
    }
    return 0;
}


你可能感兴趣的:(uva)