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 ...
随机推荐
- 实现多线程的另一种方式-Callable
package com.mldn.thread; import java.util.concurrent.ExecutionException; import java.util.concurrent ...
- MySQL Innodb 存储引擎学习篇
master thread的县城优先级别最高.其内部由几个循环(loop)组成:主循环(loop).后台循环(background loop).刷新循环(flush loop).暂停循环(suspen ...
- CentOS的update-grub2命令
这个和Ubuntu还是有些区别,在CentOS修改成如下: grub2-mkconfig -o /boot/grub2/grub.cfg
- 浏览器数据库IndexedDB介绍
摘要 在移动端H5页面开发的时候,为了更好的提高用户体验,可以对不常变化的数据做浏览器端数据缓存,在用户打开页面的时候,首先加载本地的数据,然后异步请求服务端,更新数据.在移动端webview中,可以 ...
- 使用Bootstrap 3开发响应式网站实践02,轮播
本篇体验图片轮播.html部分为: <div class="carousel slide" id="myCarousel" > <!--Ind ...
- linux 查找文件命令
find -name 文件名 在当前目录下查找 find -name nginx.conf
- ios 中局部变量可以通过传递来进行管理和释放,借此可提高代码的內聚度
ios 中 局部变量可以通过传递来进行管理和释放,通过多使用局部变量,可以提高代码的內聚度.如下: -(void)someMethod { UILabel *label = [[UILabel al ...
- 在Windows下编译Emacs
在Windows下编译Emacs Windows下编译好的Emacs主要有两个版本,一个来自http://nqmacs.sourceforge.net/,另一个来自http://www.crasseu ...
- 在windows上搭建C语言开发环境——借助eclipse和MinGW
0. 前言 [本文目的] 近期在电脑上又一次安装了MinGW,发现MinGW的安装方法和之前的方法稍有差别,全部再写了一篇博文记录一下具体的安装方法. [本文主要内容] ...
- 环境变量篇getenv putenv setenv
getenv(取得环境变量内容) 相关函数 putenv,setenv,unsetenv 表头文件 #include<stdlib.h> 定义函数 char * getenv(const ...