题意:有n张牌,每次把第一张丢掉,把第二张放到牌底,反复操作直至剩一张牌。按顺序输出丢掉牌的序号,和最后一张剩余牌的序号。
思路:很明显是用队列来模拟一下。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)
int n;
int main()
{
while(cin>>n)
{
bool out = false;
if ( !n ) break;
queue<int> q;
rep(i,1,n) q.push(i);
printf("Discarded cards:");
while( q.size() > 1 )
{
if ( out ) putchar(',');
out = true;
cout<<" "<<q.front();
q.pop();
int t = q.front();
q.pop();
q.push(t);
}
puts("");
cout<<"Remaining card: "<<q.front()<<endl;
}
return 0;
}