UVA 10935 Throwing cards away I (queue)

ゝ一纸荒年。 2022-06-07 02:00 175阅读 0赞

Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation 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 to the bottom of the deck.
Your task is to find the sequence of discarded cards and 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 not be processed.

Output For each number from the input produce two lines of output. The first line presents the sequence of discarded cards, the second line reports the last remaining card. No line will have leading or trailing 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

Discarded cards: 1, 3, 5, 2, 6

Remaining card: 4

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdlib>
  4. #include <queue>
  5. using namespace std;
  6. int main()
  7. {
  8. int n;
  9. while(cin>>n&&n)
  10. {
  11. queue <int> a;
  12. for(int i=1;i<=n;++i)//若i为1,则直接执行两个cout语句
  13. a.push(i);
  14. cout<<"Discarded cards:";
  15. int flag=0;
  16. while(a.size()>1)
  17. {
  18. if(flag)
  19. cout<<",";
  20. cout<<" "<<a.front();
  21. a.pop();
  22. a.push(a.front());
  23. a.pop();
  24. flag=1;
  25. }
  26. cout<<endl<<"Remaining card: "<<a.front()<<endl;
  27. }
  28. return 0;
  29. }

发表评论

表情:
评论列表 (有 0 条评论,175人围观)

还没有评论,来说两句吧...

相关阅读

    相关 I - Fire! UVA - 11624

    Think: 1两点BFS,第一遍预处理火到每个结点的时间,第二遍遍历判断是否可以走出迷宫,以及得到走出迷宫的最短时间 2注意时间和数组越界 [vjudge题目链接]