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 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)的更多相关文章
- 紫书 例题 11-13 UVa 10735(混合图的欧拉回路)(最大流)
这道题写了两个多小时-- 首先讲一下怎么建模 我们的目的是让所有点的出度等于入度 那么我们可以把点分为两部分, 一部分出度大于入度, 一部分入度大于出度 那么显然, 按照书里的思路,将边方向后,就相当 ...
- 10年省赛-Greatest Number (二分+暴力) + 12年省赛-Pick apples(DP) + UVA 12325(暴力-2次枚举)
题意:给你n个数,在里面取4个数,可以重复取数,使和不超过M,求能得到的最大的数是多少: 思路:比赛时,和之前的一个题目很像,一直以为是体积为4(最多选择四次)的完全背包,结果并不是,两两求和,然后二 ...
- 紫书 例题 10-26 UVa 11440(欧拉函数+数论)
这里用到了一些数论知识 首先素因子都大于M等价与M! 互质 然后又因为当k与M!互质且k>M!时当且仅当k mod M! 与M!互质(欧几里得算法的原理) 又因为N>=M, 所以N!为M! ...
- Euler Circuit UVA - 10735(混合图输出路径)
就是求混合图是否存在欧拉回路 如果存在则输出一组路径 (我就说嘛 咱的代码怎么可能错.....最后的输出格式竟然w了一天 我都没发现) 解析: 对于无向边定向建边放到网络流图中add(u, v, 1) ...
- 紫书 例题7-14 UVa 1602(搜索+STL+打表)
这道题想了很久不知道怎么设置状态,怎么拓展,怎么判重, 最后看了这哥们的博客 终于明白了. https://blog.csdn.net/u014800748/article/details/47400 ...
- Colossal Fibonacci Numbers! UVA - 11582(快速幂,求解)
Problem Description The i’th Fibonacci number f(i) is recursively defined in the following way: •f(0 ...
- 紫书 习题 11-10 UVa 12264 (二分答案+最大流)
书上写的是UVa 12011, 实际上是 12264 参考了https://blog.csdn.net/xl2015190026/article/details/51902823 这道题就是求出一种最 ...
- uva 1151(最小生成树,枚举子集)
题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此,你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐,可以 ...
- uva 818 (位运算 + 判环)
Cutting Chains What a find! Anna Locke has just bought several links of chain some of which may be ...
随机推荐
- poj 3270 置换
poj 置换的应用 黑书原题P248 /** 题意: 给定序列, 将其按升序排列, 每次交换的代价是两个数之和, 问代价最小是多少 思路:1.对于同一个循环节之内的,肯定是最小的与别的交换代价最小 2 ...
- 深度优先搜索算法(DFS)以及leetCode的subsets II
深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...
- C# DateTime.Now 用法小记
1.DateTime.Now 获取时间跟系统当前时间一直并且格式一直,如系统时间带有星期几,获取的时间也会带有 2,以下为拷贝前人总结的: //2008年4月24日 System.DateTime ...
- C#第三方zip解压压缩工具,带事例源码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using ICSharpCode. ...
- 【转载】设置event.cancelBubble,使触发子元素的onclick不同时触发父元素的onclick
由于HTML中的对象都是层次结构,比如一个Table包含了多个TR,一个TR包含了多个TD Bubble就是一个事件可以从子节点向父节点传递,比如鼠标点击了一个TD,当前的event.srcEleme ...
- N个任务掌握java系列之统计一篇文章中单词出现的次数
问题:统计一篇文章中单词出现的次数 思路: (1)将文章(一个字符串存储)按空格进行拆分(split)后,存储到一个字符串(单词)数组中. (2)定义一个Map,key是字符串类型,保存单词:valu ...
- 空类的默认函数—— SAP电面(2)/FEI
定义一个空类 class Empty { }; 默认会生成以下几个函数 2. 拷贝构造函数 Empty(const Empty& copy) { } 3. 赋值运算符 Empty& o ...
- ThinkPHP第十八天(Widget类的使用,连贯操作where IN用法,缓存S函数使用)
1.Widget类的使用方法: 第一步:在Action同级目录中新建Widget文件夹(独立分组需要自己建立) 第二步:根据不同功能在Widget文件夹中建立不同的Widget类,如热门文章HotWi ...
- MySql 数据库定时备份
1.使用sqldump+任务计划 mysqldump备份成sql文件==============假想环境:MySQL 安装位置:C:\MySQL论坛数据库名称为:bbsMySQL root 密 ...
- Symfony命令行
Available commands: help 显示命令的帮助信息 list ...