Uva 10935 Throwing cards away I
题目意思:有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的更多相关文章
- 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 ...
- UVa 10935 Throwing cards away I【队列】
题意:给出 n张牌,从上往下编号依次为1到n,当牌的数目至少还剩下2张时,把第一张牌扔掉,然后把新的一张牌放在牌堆的最底部,问最后剩下的那一张牌是哪一张牌. 模拟队列的操作------- #inclu ...
- 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 ...
- 【UVA】10935 Throwing cards away I(STL队列)
题目 题目 分析 练习STL 代码 #include <bits/stdc++.h> using namespace std; int main() { int n; wh ...
- 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 ...
- 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 ...
- 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 ...
- [刷题]算法竞赛入门经典(第2版) 5-3/UVa10935 - Throwing cards away I
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa10935 - Throwing cards away I #incl ...
- 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 / ...
随机推荐
- oralce11g 注冊表卸载20140810
Windows Registry Editor Version 5.00 [-HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE] [-HKEY_LOCAL_MACHINE\SOFT ...
- JS HTML DOM
HTML DOM (文档对象模型) 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). HTML DOM 模型被构造为对象的树. HTML DOM 树 通过 ...
- 【Android界面实现】使用Canvas对象实现“刮刮乐”效果
在淘宝.京东等电商举办活动的时候,常常能够看到在移动client推出的各种刮奖活动,而这样的活动也受到了非常多人的喜爱.从client的体验来说,这样的效果应该是通过网页来实现的,那么,我们使用And ...
- CSS文字样式
font-family:通常文章的正文使用的是易读性较强的serif字体,用户长时间阅读下不easy疲劳.而标题和表格则採用较醒目的sans-serif字体.Web设计及浏览器设置中也推荐遵循此原则. ...
- Css Rest 方法
在当今网页设计/开发实践中,使用CSS来为语义化的(X)HTML标记添加样式风格是 重要的关键.在设计师们的梦想中都存在着这样的一个完美世界:所有的浏览器都能够理解和适用多有CSS规则,并且呈现相同的 ...
- 新浪微博 2.4sdk 一闪而过
解决方法,保持 ios应用中的 build id和 开放平台中填写的一致
- JavaScript之字符串引号的使用技巧
在JavaScript中可以随意使用引号,但是最好根据字符串包含的字符来选择. 1.如果字符串里面包含了单引号,那就把字符串放在双引号里面 var age = "this is 'pig'? ...
- AeroSpike 记录
1.基本概念: namespace:类似关系型数据库中的schema,这个需要在配置文件中配置,可以指定存储引擎.存储大小.备份数.存活时间等 set:类似关系型数据库中的表 record:类似关系型 ...
- linux里source、sh、bash、./有什么区别
在linux里,source.sh.bash../都可以执行shell script文件,那它们有什么不同吗? ----------- 1.source source a.sh 在当前shell内去读 ...
- 解决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 ...