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

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

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

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

/*
UvaOJ 10935
Emerald
Sat 2 May 2015
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std; queue <int> q; void Init( int N ) {
while( !q.empty() ) {
q.pop();
}
for( int i=1; i<N+1; i++ ) {
q.push( i );
}
} void Throw( ) {
printf( "Discarded cards: %d", q.front() );
q.pop();
q.push( q.front() );
q.pop();
while( q.size() >= 2 ) {
printf( ", %d", q.front() );
q.pop();
q.push( q.front() );
q.pop();
}
printf( "\n" );
printf( "Remaining card: %d\n", q.front() );
} int main() {
int N;
while( cin >> N && N ) {
Init( N );
if( N==1 ) {
printf( "Discarded cards:\n" );
printf( "Remaining card: 1\n" );
} else {
Throw();
}
}
return 0;
}

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. 对Devexpress ASP.NET组件的一些看法

    使用.net开发的应该都熟悉DevExpress这套组件,强大的功能,显著提高开发效率和提升用户体验. 不过好像大都用winform, 说起用asp.net组件来开发webform,很多人开口就说慢, ...

  2. Ubuntu_16.04 配置 Apache Rwrite URL 重写

    Ubuntu Apache配置Rwrite URL重写 0. apache目录

  3. Structs

    1.服务端的运行程序 2.Servlet的三个方法 init service:抽象方法 destroy 3.步骤 (1).在web.xml中 <servlet> <servlet-n ...

  4. 采用translate实现垂直水平居中

    今天分享一个利用css3新特性实现垂直水平居中的方法. 通过对元素进行绝对定位再配合transform中的translate实现. 代码如下: html <div id="conten ...

  5. MYSQL存储过程事务列子

    CREATE DEFINER=`root`@`localhost` PROCEDURE `createBusiness`(parameter1 int) BEGIN #Routine body goe ...

  6. 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本、内核、当前时间

    申请博客有一段时间了,然而到现在还一篇没有写过..... 主要因为没有想到需要写些什么,最近在学习Python语言,照着书上看了看最基础的东西,发现根本看不进去,而且光看的话今天看了觉得都理解懂了,过 ...

  7. HTML+CSS笔记 CSS进阶再续

    CSS的布局模型 清楚了CSS 盒模型的基本概念. 盒模型类型, 我们就可以深入探讨网页布局的基本模型了.布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之上, ...

  8. c# zip file and folder programmatically

    In .net 4.5 Framework, we can zip a file by this way: private static string CompressFile(string sour ...

  9. js 中的bind函数

    bind是Function.prototype中内置函数 作用是指定函数作用域 代码参考 http://blog.csdn.net/load_life/article/details/7200381 ...

  10. HDU 3350 #define is unsafe

    题目大意:给定一个只含有MAX和+操作的式子,求加法运行了多少次,其中MAX使用宏定义. 题解:注意一个规律,对于MAX(A,B)其中A中加a次,B中加b次若A>B,则加a*2+b次,否则a+b ...