题目:https://vjudge.net/problem/UVA-10935
思路:用deque模拟即可,注意n=1时的格式问题,让我格式错误了一次。
代码:
#include <iostream>
#include <string>
#include <sstream>
#include <cstdio>
#include <iomanip>
#include <map>
#include <set>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <deque>
using namespace std;
int main()
{
int n;
while(cin >> n)
{
if(n==0)
{
break;
}
deque<int> card;
for(int i=1; i<=n; i++)
{
card.push_back(i);
}
cout << "Discarded cards:" << flush;
while(card.size()>1)
{
if(card.size()==n)
{
printf(" %d", card[0]);
}
else
{
printf(", %d", card[0]);
}
card.pop_front();
card.push_back(card[0]);
card.pop_front();
}
cout << endl;
printf("Remaining card: %d\n", card[0]);
}
return 0;
}