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 / ...
随机推荐
- linux网络编程涉及的函数
常用的网络命令: netstat 命令netstat是用来显示网络的连接,路由表和接口统计等网络的信息. netstat有许多的选项我们常用的选项是-an用来显示详细的网络状态.至于其它选项我们使用帮 ...
- ASPxGridView后台获取edit、delete、选择框等按钮。
GridViewCommandColumn commandColumn = (GridViewCommandColumn)ASPxGridViewInstance.Columns["#&qu ...
- 帝国cms分页样式修改文件-注意事项
帝国cms分页样式主要有:内容页分页样式.列表页分页样式以及默认搜索模板使用的搜索样式等几种. 要改这些样式其实也很简单,在网站目录中找到相应的.css文件修改empages属性就行了,但是这样比较麻 ...
- 自己动手写List集合(C#)
平时经常使用微软的List集合,觉得理所应当,这阵子突然意识到学编程学这么久,总不能只生存在某个平台某种语言下面.我觉得要跳出这个框,而数据结构是经常用到的,所以呢,作为一个有志向的程序员应该学会它. ...
- 64位linux下安装oracle10 64位 遇到 :ins_ctx.mk ;ins_emdb.mk
http://blog.csdn.net/bamuta/article/details/10523835 http://www.cnblogs.com/kerrycode/p/3519446.html ...
- JS实现信息的显示和隐藏
JS实现信息的显示和隐藏 我们在写注册页面的时候,必填信息是可见的,可选信息是隐藏的,如果用户希望填写,可以单击“详细信息”. 代码如下:<!DOCTYPE html><html&g ...
- HDU 2147 kiki's game
题解:画图可得当横纵坐标均为奇数时为必败态…… #include <cstdio> int main(){ int a,b; while(scanf("%d%d",&a ...
- Android Manifest.xml 结构详解
关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activities ...
- poj 1077 Eight(双向bfs)
题目链接:http://poj.org/problem?id=1077 思路分析:题目要求在找出最短的移动路径,使得从给定的状态到达最终状态. <1>搜索算法选择:由于需要找出最短的移动路 ...
- Desert King(最优比率生成树)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22717 Accepted: 6374 Desc ...