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

要求输出:依次输出被扔掉的牌,按扔掉的顺序输出。最后要输出最后留下的一张牌。

思路:用一个队列来模拟,被扔掉的牌相当于取出后进行pop操作,把最上面的牌放置最后相同于取出后进行pop操作和push操作,直至队列的size小于等于1

注意点:因为题目对格式的要求,所以第一张被扔掉的牌格式处理上于后面被扔掉的牌有所不同。

  1. /*
  2. UvaOJ 10935
  3. Emerald
  4. Sat 2 May 2015
  5. */
  6. #include <iostream>
  7. #include <cstring>
  8. #include <cstdio>
  9. #include <queue>
  10.  
  11. using namespace std;
  12.  
  13. queue <int> q;
  14.  
  15. void Init( int N ) {
  16. while( !q.empty() ) {
  17. q.pop();
  18. }
  19. for( int i=1; i<N+1; i++ ) {
  20. q.push( i );
  21. }
  22. }
  23.  
  24. void Throw( ) {
  25. printf( "Discarded cards: %d", q.front() );
  26. q.pop();
  27. q.push( q.front() );
  28. q.pop();
  29. while( q.size() >= 2 ) {
  30. printf( ", %d", q.front() );
  31. q.pop();
  32. q.push( q.front() );
  33. q.pop();
  34. }
  35. printf( "\n" );
  36. printf( "Remaining card: %d\n", q.front() );
  37. }
  38.  
  39. int main() {
  40. int N;
  41. while( cin >> N && N ) {
  42. Init( N );
  43. if( N==1 ) {
  44. printf( "Discarded cards:\n" );
  45. printf( "Remaining card: 1\n" );
  46. } else {
  47. Throw();
  48. }
  49. }
  50. return 0;
  51. }

Uva 10935 Throwing cards away I的更多相关文章

  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,当牌的数目至少还剩下2张时,把第一张牌扔掉,然后把新的一张牌放在牌堆的最底部,问最后剩下的那一张牌是哪一张牌. 模拟队列的操作------- #inclu ...

  3. uva 10935 throwing cards away <queue>

    Given is an ordered deck of    n    cards numbered 1 to    n    with card 1 at the top and card    n ...

  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. 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 ...

  7. 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 ...

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

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

  9. 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 / ...

随机推荐

  1. oralce11g 注冊表卸载20140810

    Windows Registry Editor Version 5.00 [-HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE] [-HKEY_LOCAL_MACHINE\SOFT ...

  2. JS HTML DOM

    HTML DOM (文档对象模型) 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). HTML DOM 模型被构造为对象的树. HTML DOM 树 通过 ...

  3. 【Android界面实现】使用Canvas对象实现“刮刮乐”效果

    在淘宝.京东等电商举办活动的时候,常常能够看到在移动client推出的各种刮奖活动,而这样的活动也受到了非常多人的喜爱.从client的体验来说,这样的效果应该是通过网页来实现的,那么,我们使用And ...

  4. CSS文字样式

    font-family:通常文章的正文使用的是易读性较强的serif字体,用户长时间阅读下不easy疲劳.而标题和表格则採用较醒目的sans-serif字体.Web设计及浏览器设置中也推荐遵循此原则. ...

  5. Css Rest 方法

    在当今网页设计/开发实践中,使用CSS来为语义化的(X)HTML标记添加样式风格是 重要的关键.在设计师们的梦想中都存在着这样的一个完美世界:所有的浏览器都能够理解和适用多有CSS规则,并且呈现相同的 ...

  6. 新浪微博 2.4sdk 一闪而过

    解决方法,保持 ios应用中的 build id和 开放平台中填写的一致

  7. JavaScript之字符串引号的使用技巧

    在JavaScript中可以随意使用引号,但是最好根据字符串包含的字符来选择. 1.如果字符串里面包含了单引号,那就把字符串放在双引号里面 var age = "this is 'pig'? ...

  8. AeroSpike 记录

    1.基本概念: namespace:类似关系型数据库中的schema,这个需要在配置文件中配置,可以指定存储引擎.存储大小.备份数.存活时间等 set:类似关系型数据库中的表 record:类似关系型 ...

  9. linux里source、sh、bash、./有什么区别

    在linux里,source.sh.bash../都可以执行shell script文件,那它们有什么不同吗? ----------- 1.source source a.sh 在当前shell内去读 ...

  10. 解决spark运行中failed to locate the winutils binary in the hadoop binary path的问题

    1.下载hadoop-common-2.2.0-bin并解压到某个目录 https://github.com/srccodes/hadoop-common-2.2.0-bin 2.设置hadoop.h ...