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.

Each line of input (except the last) contains a number     n     ≤ 50. The last line contains 0 and this line should not be processed. 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

  1. 7
  2. 19
  3. 10
  4. 6
  5. 0

Output for sample input

  1. Discarded cards:1,3,5,7,4,2
  2. Remaining card:6
  3. Discarded cards:1,3,5,7,9,11,13,15,17,19,4,8,12,16,2,10,18,14
  4. Remaining card:6
  5. Discarded cards:1,3,5,7,9,2,6,10,8
  6. Remaining card:4
  7. Discarded cards:1,3,5,2,6
  8. Remaining card:4
  9.  
  10. 这道题超简单,五分钟一次写完运行成功
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. queue<int> cards;
  7. int main()
  8. {
  9. int n;
  10. while( scanf( "%d", &n ) == ){
  11. if(n == ) break;
  12. for(int i = ;i <= n; i++)cards.push(i);
  13.  
  14. printf("Discarded cards:");
  15. int t;
  16. for(int i = ;i < ; i++){
  17. if(i == )cout << cards.front();
  18. else cout << "," <<cards.front();
  19. cards.pop();
  20. t = cards.front();
  21. cards.pop();
  22. if(cards.empty())break;
  23. cards.push(t);
  24. }
  25. printf("\nRemaining card:%d\n",t);
  26. }
  27. //system("pause");
  28. return ;
  29. }

uva 10935 throwing cards away <queue>的更多相关文章

  1. UVa 10935 - Throwing cards away I (队列问题)

    原题 Throwing cards away I   Given is an ordered deck of n cards numbered 1 to n with card 1 at the to ...

  2. Uva 10935 Throwing cards away I

    题目意思:有N张牌,标号为1~N,且牌以叠好,从上到小就是标号1-N的牌,只要牌堆数量大于等于2的时候,就采取如下操作:将最上面的牌扔掉(即离开牌堆).刚才那张牌离开后,再将新的最上面的牌放置于牌堆最 ...

  3. UVa 10935 Throwing cards away I【队列】

    题意:给出 n张牌,从上往下编号依次为1到n,当牌的数目至少还剩下2张时,把第一张牌扔掉,然后把新的一张牌放在牌堆的最底部,问最后剩下的那一张牌是哪一张牌. 模拟队列的操作------- #inclu ...

  4. 【UVA】10935 Throwing cards away I(STL队列)

    题目 题目     分析 练习STL     代码 #include <bits/stdc++.h> using namespace std; int main() { int n; wh ...

  5. UVA 10940 Throwing cards away II

    题意略: 先暴力打表发现规律 N=1 ans=1N=2 ans=2N=3 ans=2N=4 ans=4N=5 ans=2N=6 ans=4N=7 ans=6N=8 ans=8N=9 ans=2N=10 ...

  6. UVa---------10935(Throwing cards away I)

    题目: Problem B: Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 ...

  7. [刷题]算法竞赛入门经典(第2版) 5-3/UVa10935 - Throwing cards away I

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa10935 - Throwing cards away I #incl ...

  8. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  9. Throwing cards away I

    Throwing cards away I   Given is an ordered deck of n cards numbered 1 to n with card 1 at the top a ...

随机推荐

  1. 吸血鬼数字算法参考 -- javascript版本

    // 吸血鬼数字 java编程思想 第四章 75页 练习10 for (var i = 10; i <= 99; i++) { for (var j = i + 1; j < 99; j+ ...

  2. Bootstrap 字形图标(Glyphicons)

    http://w3c.3306.biz/bootstrap/eg/bootstrap--glyphicons-list.html

  3. poj1915 BFS

    D - 广搜 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:30000KB     64bi ...

  4. 洛谷 P3383 【模板】线性筛素数

    P3383 [模板]线性筛素数 题目描述 如题,给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内) 输入输出格式 输入格式: 第一行包含两个正整数N.M,分别表示查询的范 ...

  5. [Database] Deadlock avoidance protocol

    如何避免Deadlock,如果我们能提前知道各个Process对于资源的需求情况,我们就可以用Banker's algorithm (银行家算法) 来解决问题.可是这在现在中不好实现,因为很难提前知道 ...

  6. java基础知识再学习--HashMap与ConcurrentHashMap的区别

    引用:http://blog.csdn.net/xuefeng0707/article/details/40834595 从JDK1.2起,就有了HashMap,正如前一篇文章所说,HashMap不是 ...

  7. memcached的安装

    最近在研究怎么让Discuz!去应用Memcache去做一些事情,记录下Memcache安装的过程. Linux下Memcache服务器端的安装 服务器端主要是安装memcache服务器端,目前的最新 ...

  8. INI文件格式

    最近在看git命令,遇到INI文件格式,上网查了一下,把它总结一下: 程序没有任何配置文件,那么它对外是全封闭的,一旦程序需要修改一些参数必须要修改程序代码本身并重新编译,为了让程序出厂后还能根据需要 ...

  9. win7自由调整CMD窗口

    有如下命令,只需要改动相关参数即可以任意改变cmd窗口大小. mode con lines= mode con cols= color cls @cmd

  10. 在C51中如何实现软复位?

    可以定义一个指向复位向量(0x0000)的函数指针,然后在C程序中需要软复位的地方调用该函数: ((void (code *) (void)) 0x0000) (); 例如,以下程序不断地复位: vo ...