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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. [刷题]算法竞赛入门经典(第2版) 5-3/UVa10935 - Throwing cards away I

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa10935 - Throwing cards away I #incl ...

  5. 紫书第五章训练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 ...

  6. UVA10940 - Throwing cards away II(找到规律)

    UVA10940 - Throwing cards away II(找规律) 题目链接 题目大意:桌上有n张牌,依照1-n的顺序从上到下,每次进行将第一张牌丢掉,然后把第二张放到这叠牌的最后.重复进行 ...

  7. UVa 10935 (水题) Throwing cards away I

    直接用STL里的queue模拟即可. #include <cstdio> #include <queue> using namespace std; ; int discard ...

  8. UVa 10935 Throwing cards away I【队列】

    题意:给出 n张牌,从上往下编号依次为1到n,当牌的数目至少还剩下2张时,把第一张牌扔掉,然后把新的一张牌放在牌堆的最底部,问最后剩下的那一张牌是哪一张牌. 模拟队列的操作------- #inclu ...

  9. 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 ...

随机推荐

  1. 理解JavaScript中BOM和DOM的关系

    JavaScript 有三部分构成,ECMAScript,DOM和BOM,根据宿主(浏览器)的不同,具体的表现形式也不尽相同,IE和其他的浏览器风格迥异.对象是JavaScript最重要的API,包含 ...

  2. GCC 对C语言的扩展

    http://www.cnblogs.com/emituofo/archive/2012/07/20/2600995.html http://blog.csdn.net/andyhuabing/art ...

  3. cloudstack openstack zstack

    http://www.cnblogs.com/skyme/archive/2013/06/06/3118852.html http://www.niubua.com/ http://zstack.or ...

  4. java基础学习总结——哈希编码

    一.哈希编码

  5. ARCGIS将WGS84坐标投影到高斯平面

    将WGS84坐标投影到平面,一般採用的是UTM(通用横轴莫卡托投影).该方式多用于美国地区,而我国多用北京54和西安80高斯克吕格投影坐标.假如我们想把影像採用高斯克吕格投影到在平面上,而ARCGIS ...

  6. 转:如何解决“My mac 64-bit”问题

    童鞋们都知道Xcode会根据当前SDK在Run按钮旁边的选项栏中显示适合的Simulator供开发者选择,如下图: 但是有时候则错误显示“My mac 64-bit” ,这个明显不是我们想要的,如下图 ...

  7. 树状结构 Tree data structure in C#

    delegate void TreeVisitor<T>(T nodeData); class NTree<T> { private T data; private Linke ...

  8. wpf z

    Finding an ancestor of a WPF dependency object This is a simple snippet which helps you to find a sp ...

  9. MSI failed, 不能卸载VMware

    解决方法; http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&ext ...

  10. 《C和指针》整理一

    1.C语言的凝视     在C语言中,假设须要凝视掉一段代码.且代码中可能会已经存在/**/凝视形式,那么能够使用: #if 0     statements #endif     这样的形式来凝视掉 ...