紫书第五章训练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 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.
Input Each line of input (except the last) contains a number n ≤ 50. The last
line contains ‘0’ and this line should not be processed.
Output 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
Sample Output
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张牌,先把第一张扔掉,然后把第一张放在最后,然后再把第一张牌扔掉,剩一张牌时再输出你扔掉的这张牌。本来想到要用数组模拟,类似于约瑟夫环。但是GG,自己试了几组并不对,然后自己就被晾在那了,改了很久的数组突然发现很多人AC了,我就想也许数组比较麻烦,我需要抖机灵想些别的数据结构,一拍大腿这就是队列啊,哭晕在厕所。哎,这种典型的队列和括号匹配所代表的栈自己得十分熟悉才行啊,自动hash匹配啊。有种回到高考的感觉,但是高考我可没这么努力啊。
#include<iostream>
using namespace std;
int s[];
int n;
int main()
{ while(cin>>n&&n) { int k=;
int l=n;
for(int i=; i<=n; i++)s[i]=i;
if(n==)
cout<<"Discarded cards:"<<endl<<"Remaining card: 1"<<endl;
else
{
cout<<"Discarded cards: ";
while(k<l)
{
cout<<s[k];
k++;
if(k<l)
cout<<", ";
if(k==l)
cout<<endl<<"Remaining card: "<<s[k]<<endl;
s[++l]=s[k];
k++;
}
}
}
return ;
}
紫书第五章训练3 D - Throwing cards away I的更多相关文章
- 紫书第五章训练2 F - Compound Words
F - Compound Words You are to find all the two-word compound words in a dictionary. A two-word compo ...
- 紫书第三章训练1 D - Crossword Answers
A crossword puzzle consists of a rectangular grid of black and white squares and two lists of defini ...
- 紫书第三章训练1 E - DNA Consensus String
DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of ...
- GAN实战笔记——第五章训练与普遍挑战:为成功而GAN
训练与普遍挑战:为成功而GAN 一.评估 回顾一下第1章中伪造达・芬奇画作的类比.假设一个伪造者(生成器)正在试图模仿达・芬奇,想使这幅伪造的画被展览接收.伪造者要与艺术评论家(判别器)竞争,后者试图 ...
- OpenGL蓝宝书第五章代码勘误以及惯性坐标系去解释模型变换:Pyramid.cpp
假设你也发现依照教程代码完毕贴图时,你会底面的坐标和寻常顶点坐标正负相反,比方-1.0f, -1.0f, -1.0f这个顶点相应的却是世界坐标中1.0f,-1.0f,1.0f 问题到底出如今哪里? 原 ...
- 紫书第5章 C++STL
例题 例题5-1 大理石在哪儿(Where is the Marble?,Uva 10474) 主要是熟悉一下sort和lower_bound的用法 关于lower_bound: http://blo ...
- 小红书第五章——引用类型之function类型
有趣的函数——function类型 函数实际上是对象,每个函数都是function类型的实例,具有属性和方法. 1.函数的定义方法 1)函数声明语法 function sum(num1,num2){/ ...
- 《汇编语言 基于x86处理器》前五章的小程序
▶ 书中前五章的几个小程序,基本的运算操作,使用了作者的库 Irvine32 和 Irvine64(一开始以为作者网站过期了,各网站上找到的文件大小都不一样,最后发现是要搭梯子 Orz,顺利下载).注 ...
- ROS机器人程序设计(原书第2版)补充资料 (伍) 第五章 计算机视觉
ROS机器人程序设计(原书第2版)补充资料 (伍) 第五章 计算机视觉 书中,大部分出现hydro的地方,直接替换为indigo或jade或kinetic,即可在对应版本中使用. 计算机视觉这章分为两 ...
随机推荐
- CentOS远程监控
近日,因工作需要,学习了CentOS远程监控的水平有限,多指教. 远程访问CentOS,包括三种方式ssh,telnet,vnc. 本例涉及的是以vnc远程访问CentOS.指令在root下操作.注意 ...
- Django模板导入及母版继承和自定义返回Html片段
1.ROOT_URLCONF = '总路由所在路径(比如untitled.urls)'<===默认情况是这样 根路由的路径是可以修改的:ROOT_URLCONF = app01.urls< ...
- 精通AngularJS(三)深入scope,继承结构,事件系统和生命周期
深入探讨 Scope 作用域 每一个 $scope 都是类 Scope 的一个实例.类 Scope 拥有可以控制 scope 生命周期的方法,提供事件传播的能力,并支持模板渲染. 作用域的层次结构 让 ...
- SQL 时间日期函数
1.获取当前日期GetDate getdate()函数以datetime数据类型的格式返回当前SQLServer服务器所在计算机的日期和时间.其语法格式为getdate().返回值舍入到最近的秒小数部 ...
- SQLite C/C++ 教程
目录 1安装 2 C/C++ Interface APIs 3连接到数据库 4创建表 5插入操作 6更新操作 7删除操作 安装 在我们开始使用SQLite在C / C++程序,我们需要确保SQLite ...
- 【图文并茂】DEV配置NTL库
一开始根据陈老师的博客:再说如何在DEV C++中搭建NTL库 后来发现自己虽然跟着一步一步来做,还是做错了很多回. 究竟怎么一回事呢?后来我发现一个致命的错误很容易忽视,所以我就重新写一篇博客. 来 ...
- eclipse 在写XML时 包类名自动提醒的问题
需要加一个STS插件 配置很简单 参考了 https://blog.csdn.net/HH775313602/article/details/70176531 在 https://spring.io ...
- centos7设置sshd端口,firewall,selinux设置
https://blog.csdn.net/qq_31927797/article/details/81095829 #停止firewallsystemctl stop firewalld.servi ...
- PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)
http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...
- Bootstrap 翻页(pager)
如果您想要创建一个简单的分页链接为用户提供导航,可以通过翻页来实现.与分布链接一样,也是一个无序列表.默认情况下,翻页是居中显示的.下面列出了bootstrap处理翻页的类. Class 描述 示例代 ...