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 show indexes 查看索引状态
查看表中有哪些已建立的索引 SHOW INDEX FROM tbl_name [FROM db_name] SHOW INDEX会返回以下字段: | Table | Non_unique | Key ...
- 不错的SDL源码分析
SDL源码分析 1:初始化(SDL_Init()) 2:窗口(SDL_Window) 3:渲染器(SDL_Renderer) 4:纹理(SDL_Texture) 5:更新纹理(SDL_UpdateT ...
- lamp经典安装
一.网络方面的知识 2 ①-网络常见的命令 2 ②-网卡相关 2 ③-防火墙相关 2 ④-selinux相关 3 二.上传amp源代码包 5 三.linux下软件安装-vsftpd安装 6 ①-rpm ...
- FastBoot BootLoader Recovery 模式解释
理论上,所有的Android设备都存在着Fastboot/Bootloader模式,不过,由于Android操作系统的开源特性,各厂商的对 自家的相关Android设备都有着各自不同的Fastboot ...
- java的(PO,VO,TO,BO,DAO,POJO)解释1
java的(PO,VO,TO,BO,DAO,POJO)解释 O/R Mapping 是 Object Relational Mapping(对象关系映射)的缩写.通俗点讲,就是将对象与关系数据库绑定 ...
- LINUX 性能工具使用
http://xuclv.blog.51cto.com/5503169/1184517
- ASP.NET web.config中<customErrors>节点说明
customErrors>节点用于定义一些自定义错误信息的信息.此节点有Mode和defaultRedirect两个属性,其中defaultRedirect属性是一个可选属性,表示应用程序发生错 ...
- [Asp.net MVC]页面伪静态实现
摘要 从页面Url及页面名称上看,你会发现静态页面和伪静态是一样的.伪静态的页面后缀可能是html,htm,cshtml等,只是改变了url的表现形式,实际上还是动态的页面.在SEO方面,伪静态和静态 ...
- 使用jquery加载部分视图02-使用$.ajax()
本篇体验使用$.ajax()加载部分视图.与加载部分视图相关的包括: RenderPartial和RenderAction区别 使用jquery加载部分视图01-使用$.get() □ ...
- pytest文档15-使用自定义标记mark
前言 pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行.app自动化的时候,如果想android和ios公用一套代码时, 也可以使用标记功能,标明哪些是 ...