题目:

Problem B: 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 分析:直接用queue进行模拟即可,不过要注意的是当n = 1时,"Discarded cards:"后不能加空格,否则上交时会提示PE错误
代码如下:
#include<cstdio>
#include<queue>
using namespace std;
int main(){
int n;
queue<int>cards;
while(scanf("%d", &n) == && n){
bool flag = true;
//将所有纸牌放在队中
for(int i = ; i <= n; i++)
cards.push(i);
if(n == )
printf("Discarded cards:");
else
printf("Discarded cards: ");
     //模拟过程
while(cards.size() > ){
if(flag)
flag = false;
else
printf(", ");
//第一张卡片出队
printf("%d",cards.front());
cards.pop();
//新的第一张卡片到队尾
cards.push(cards.front());
cards.pop();
}
printf("\n");
printf("Remaining card: %d\n", cards.front());
cards.pop();
}
return ;
}

2015-07-04文

UVa---------10935(Throwing cards away I)的更多相关文章

  1. 紫书 例题 11-13 UVa 10735(混合图的欧拉回路)(最大流)

    这道题写了两个多小时-- 首先讲一下怎么建模 我们的目的是让所有点的出度等于入度 那么我们可以把点分为两部分, 一部分出度大于入度, 一部分入度大于出度 那么显然, 按照书里的思路,将边方向后,就相当 ...

  2. 10年省赛-Greatest Number (二分+暴力) + 12年省赛-Pick apples(DP) + UVA 12325(暴力-2次枚举)

    题意:给你n个数,在里面取4个数,可以重复取数,使和不超过M,求能得到的最大的数是多少: 思路:比赛时,和之前的一个题目很像,一直以为是体积为4(最多选择四次)的完全背包,结果并不是,两两求和,然后二 ...

  3. 紫书 例题 10-26 UVa 11440(欧拉函数+数论)

    这里用到了一些数论知识 首先素因子都大于M等价与M! 互质 然后又因为当k与M!互质且k>M!时当且仅当k mod M! 与M!互质(欧几里得算法的原理) 又因为N>=M, 所以N!为M! ...

  4. Euler Circuit UVA - 10735(混合图输出路径)

    就是求混合图是否存在欧拉回路 如果存在则输出一组路径 (我就说嘛 咱的代码怎么可能错.....最后的输出格式竟然w了一天 我都没发现) 解析: 对于无向边定向建边放到网络流图中add(u, v, 1) ...

  5. 紫书 例题7-14 UVa 1602(搜索+STL+打表)

    这道题想了很久不知道怎么设置状态,怎么拓展,怎么判重, 最后看了这哥们的博客 终于明白了. https://blog.csdn.net/u014800748/article/details/47400 ...

  6. Colossal Fibonacci Numbers! UVA - 11582(快速幂,求解)

    Problem Description The i’th Fibonacci number f(i) is recursively defined in the following way: •f(0 ...

  7. 紫书 习题 11-10 UVa 12264 (二分答案+最大流)

    书上写的是UVa 12011, 实际上是 12264 参考了https://blog.csdn.net/xl2015190026/article/details/51902823 这道题就是求出一种最 ...

  8. uva 1151(最小生成树,枚举子集)

    题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此,你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐,可以 ...

  9. uva 818 (位运算 + 判环)

     Cutting Chains  What a find! Anna Locke has just bought several links of chain some of which may be ...

随机推荐

  1. HTML编码的用户输入

    public string Browse(string genre) { returen HttpUtility.HtmlEncode(genre); } HttpUtility.HtmlEncode ...

  2. 简单测试运行时类信息(RTTI),附详细例子

    新建一个单元文件,填写如下代码,然后保存为 ClassInfoUnit.pas,这里定义了一个结构,用来读取指定类的信息. unit ClassInfoUnit; interface uses Cla ...

  3. BZOJ 1103 [POI2007]大都市meg(树状数组+dfs序)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1103 [题目大意] 给出一棵树,每条边的经过代价为1,现在告诉你有些路不需要代价了, ...

  4. oracle dataguard 角色切换

  5. Zombie.js Insanely fast, headless full-stack testing using Node.js

    (92) Is there a better crawler than Scrapy? - Quora Is there a better crawler than Scrapy?Edit Insan ...

  6. 基于视觉的Web页面分页算法VIPS的实现源代码下载

    基于视觉的Web页面分页算法VIPS的实现源代码下载 - tingya的专栏 - 博客频道 - CSDN.NET 基于视觉的Web页面分页算法VIPS的实现源代码下载 分类: 技术杂烩 2006-04 ...

  7. DOM ISO - get current element's XPATH

    DOM ISO - get current element's XPATH DOM ISO - get current element's XPATH

  8. Thread’s start method and run method

    工作中用到了Thread,一开始用错了,仔细研究了一下,稍作整理. 前言,今天写代码居然这样写的 new Thread() { @Override public void run() { System ...

  9. js小写转实现资本,js数字革命万元

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>  <hea ...

  10. HDU1250:Hat's Fibonacci

    Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...