UVa 10935 - Throwing Cards Away I(模拟)

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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;
}

本文迁移自我的 CSDN博客 ,格式可能有所偏差。

支付宝扫码领红包