Throwing cards away I uva1594
Throwing cards away I
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
7
19
10
6
0
Output for sample input
Discarded cards: 1, 3, 5, 7, 4, 2
Remaining card: 6
Discarded cards: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 4, 8, 12, 16, 2, 10, 18, 14
Remaining card: 6
Discarded cards: 1, 3, 5, 7, 9, 2, 6, 10, 8
Remaining card: 4
Discarded cards: 1, 3, 5, 2, 6
Remaining card: 4
题意:给定你一个数n,牌从1~n按顺序排列,首先把第一张牌扔掉。然后再把最上面的放到最后面。 依次循环,直到仅仅有一张牌为止。第一行输出按顺序扔掉的牌,第二行输出最后剩下的牌。
思路:本题须要用到数据结构---队列,先让第一个为head,最后一个元素的后一个为tail把第一个元素向前移动一个位置,这样就删除了第一个,然后再把第一个元素移到最后面。tail向后移动一个位置。
代码:
#include<cstdio>
using namespace std;
int q[200];
int main()
{
int head,tail,x;
while(scanf("%d",&x)!=EOF&&x!=0)
{ for(int i=1;i<=x+1;i++)
q[i]=i;
head=1;
tail=x+1;
printf("Discarded cards:");
while(head<tail-1)
{
if(head<tail-2)
printf(" %d,",q[head]);
else
printf(" %d",q[head]);
head++;
q[tail]=q[head];
tail++;
head++;
}
printf("\n");
printf("Remaining card:");
printf(" %d",q[head]);
printf("\n"); }
return 0;
}
Throwing cards away I uva1594的更多相关文章
- 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 (队列问题)
原题 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)
题目: 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 ...
- 紫书第五章训练3 D - Throwing cards away I
D - Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top ...
- UVA10940 - Throwing cards away II(找到规律)
UVA10940 - Throwing cards away II(找规律) 题目链接 题目大意:桌上有n张牌,依照1-n的顺序从上到下,每次进行将第一张牌丢掉,然后把第二张放到这叠牌的最后.重复进行 ...
- UVa 10935 (水题) Throwing cards away I
直接用STL里的queue模拟即可. #include <cstdio> #include <queue> using namespace std; ; int discard ...
- 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 ...
随机推荐
- mysql反向解析导致连接缓慢
Content 0.序 1.问题 2.原因 3.解决办法 0.序 本文主要是记录Mysql安装在 VMWARE下,本地连接Mysql速度很慢的原因及解决办法. 1.问题 本地的一个网站使用mysql数 ...
- 最小生成树-普利姆算法lazy实现
算法描述 lazy普利姆算法的步骤: 1.从源点s出发,遍历它的邻接表s.Adj,将所有邻接的边(crossing edges)加入优先队列Q: 2.从Q出队最轻边,将此边加入MST. 3.考察此边的 ...
- 模仿JQuery 的添加多个事件的原理
var jquery=function(dom){ var obj={ ready:function(fn){ if(typeof dom.onload=="function"){ ...
- vue-router query和params传参(接收参数),$router、$route的区别
链接:https://segmentfault.com/a/1190000012735168 1.query方式传参和接收参数 传参: this.$router.push({ path:'/xxx' ...
- python+Django+apache的配置
下载安装xampp套件 下载mod_python-3.3.1.win32-py2.5-Apache2.2.exe 下载python-2.5.4.msi 下载Django 下载MySQL-python- ...
- ISO7816 (part 1-3) asynchronous smartcard information
http://java.inf.elte.hu/java-1.3/javacard/iso7816.txt ============================================== ...
- 在 CentOS 和 RHEL 上安装 Puppet 服务器和客户端
https://linux.cn/article-3959-1.html https://docs.puppet.com/
- Windows程序的打包,部署(vs项目打包vs2013)---ShinePans
Windows 应用程序在开发完毕之后,怎样将程序打包并制作成安装程序在客户机上部署 是每一个windows应用程序开发完毕之后都必须面对的问题. 学习目标: 部 ...
- 安装wp8sdk 当前系统时钟或签名文件中的时间戳验证时要求的证书不在有效期内。
安装wp8sdk 当前系统时钟或签名文件中的时间戳验证时要求的证书不在有效期内. [1404:0090][2015-06-12T08:00:53]: Error 0x800b0101: Failed ...
- Oracle数据库--解决单张表中数据量巨大(大数据、数据量上百万级别,后查询,更新数据等耗时剧增)
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/QQ578473688/article/details/54561397 思路1:采用备份表 备份表中 ...