C

来源:互联网 发布:没有激素的护肤品知乎 编辑:程序博客网 时间:2024/06/13 15:11

subject

Given is an ordered deck of n cardsnumbered 1 to n with card 1 at the top and card n at the bottom. The followingoperation is performed as long as there are at least two cards in the deck:Throw away the top card and move the card that is now on the top of the deck tothe bottom of the deck. Your task is to find the sequence of discarded cardsand the last, remaining card.

Input

Each line of input (except the last)contains a number n ≤ 50. The last line contains ‘0’ and this line should notbe processed.

Output

For each number from the input produce twolines of output. The first line presents the sequence of discarded cards, thesecond line reports the last remaining card. No line will have leading ortrailing spaces. See the sample for the expected format.

Sample Input

7

19

10

6

0

Sample Output

 Discarded cards: 1, 3, 5, 7, 4, 2

 Remaining card: 6

 Discarded cards: 1, 3, 5, 7, 9, 11, 13, 15,17, 19, 4, 8, 12, 16, 2, 10, 18, 14

 Remaining card: 6

 Discarded cards: 1, 3, 5, 7, 9, 2, 6, 10, 8

 Remaining card: 4

 Discardedcards: 1, 3, 5, 2, 6

 Remaining card: 4


题目大意如下:

      给定的是1到n的一个有序的deck,上面有card 1,底部有card n。只要在甲板上至少有两张牌,就可以进行后续操作:把上面的牌扔掉,然后把现在放在平台顶部的卡片移到甲板的底部。你的任务是找出被丢弃的卡片的顺序,最后一张,剩余的卡片。

        注意一下1的情况输出就可以了。

代码如下:

  

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<queue>using namespace std;int main(){    int n;    while(cin>>n,n)    {        queue<int> q;        for(int i=1; i<=n; i++)        {            q.push(i);        }        printf("Discarded cards:");        int r=0;        while(q.size()>=2)        {            int t = q.front();            q.pop();            int k = q.front();            q.pop();            q.push(k);            if(q.size()==1)                printf(" %d",t);            else                printf(" %d,",t);        }        printf("\nRemaining card: %d\n",q.front());    }    return 0;}

原创粉丝点击