UVa 10935 - Throwing cards away I

题意

(摘自紫书)

桌上有n(n≤50)张牌,从第一张牌(即位于顶面的牌)开始,从上往下依次编号为1~n。当至少还剩下两张牌时进行以下操作:把第一张牌扔掉,然后把新的第一张牌放到整叠牌的最后。输入每行包含一个n,输出每次扔掉的牌以及最后剩下的牌。

思路

水题
STL队列

记录

之前没考虑到的PE情况:

Input
1
Output
Discarded cards:
Remaining card: 1

*(第一行行末不应该有空格)

AC代码

#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

queue<int> s;

int main()
{
    int n, i;
    while( cin >> n && n ){
        const int n_bg = n;
        for( i = 1; i <=n; i++ )
            s.push(i);
        printf("Discarded cards:");
        while( n >= 2 )
        {
            if( n != n_bg )     printf(",") ;
            printf(" %d",s.front());
            s.pop();
            n--;
            s.push(s.front());
            s.pop();
        }
        puts("");
        printf("Remaining card: %d\n",s.front());
        s.pop();
    }
    return 0;
}
posted @ 2018-01-28 11:51  JinxiSui  阅读(79)  评论(0编辑  收藏  举报