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

  1. 紫书第五章训练2 F - Compound Words

    F - Compound Words You are to find all the two-word compound words in a dictionary. A two-word compo ...

  2. 紫书第三章训练1 D - Crossword Answers

    A crossword puzzle consists of a rectangular grid of black and white squares and two lists of defini ...

  3. 紫书第三章训练1 E - DNA Consensus String

    DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of ...

  4. GAN实战笔记——第五章训练与普遍挑战:为成功而GAN

    训练与普遍挑战:为成功而GAN 一.评估 回顾一下第1章中伪造达・芬奇画作的类比.假设一个伪造者(生成器)正在试图模仿达・芬奇,想使这幅伪造的画被展览接收.伪造者要与艺术评论家(判别器)竞争,后者试图 ...

  5. OpenGL蓝宝书第五章代码勘误以及惯性坐标系去解释模型变换:Pyramid.cpp

    假设你也发现依照教程代码完毕贴图时,你会底面的坐标和寻常顶点坐标正负相反,比方-1.0f, -1.0f, -1.0f这个顶点相应的却是世界坐标中1.0f,-1.0f,1.0f 问题到底出如今哪里? 原 ...

  6. 紫书第5章 C++STL

    例题 例题5-1 大理石在哪儿(Where is the Marble?,Uva 10474) 主要是熟悉一下sort和lower_bound的用法 关于lower_bound: http://blo ...

  7. 小红书第五章——引用类型之function类型

    有趣的函数——function类型 函数实际上是对象,每个函数都是function类型的实例,具有属性和方法. 1.函数的定义方法 1)函数声明语法 function sum(num1,num2){/ ...

  8. 《汇编语言 基于x86处理器》前五章的小程序

    ▶ 书中前五章的几个小程序,基本的运算操作,使用了作者的库 Irvine32 和 Irvine64(一开始以为作者网站过期了,各网站上找到的文件大小都不一样,最后发现是要搭梯子 Orz,顺利下载).注 ...

  9. ROS机器人程序设计(原书第2版)补充资料 (伍) 第五章 计算机视觉

    ROS机器人程序设计(原书第2版)补充资料 (伍) 第五章 计算机视觉 书中,大部分出现hydro的地方,直接替换为indigo或jade或kinetic,即可在对应版本中使用. 计算机视觉这章分为两 ...

随机推荐

  1. CentOS远程监控

    近日,因工作需要,学习了CentOS远程监控的水平有限,多指教. 远程访问CentOS,包括三种方式ssh,telnet,vnc. 本例涉及的是以vnc远程访问CentOS.指令在root下操作.注意 ...

  2. 使用Google Colab训练神经网络(二)

    Colaboratory 是一个 Google 研究项目,旨在帮助传播机器学习培训和研究成果.它是一个 Jupyter 笔记本环境,不需要进行任何设置就可以使用,并且完全在云端运行.Colaborat ...

  3. urllib基础-构造请求对象,设置用户代理User-Agent

    有的网页具有一些反爬机制,如:需要浏览器请求头中的User-Agent.User-Agent类似浏览器的身份证. 程序中不设置User-Agent.默认是Python-urllib/3.5.这样网站就 ...

  4. WPF知识点全攻略09- 附加属性

    附加属性也是一种特殊的依赖属性. Canvas中的Canvas.Left,Canvas.Top ,DockPanel中DockPanel.Dock等就是附加属性. 更加.NET类属性的写法经验.这个中 ...

  5. ZendStudio 常用快捷键大全

    应用场景 快捷键 功能 查看快捷键 ctrl+shift+l 显示所有快捷键列表 查看和修改快捷键   打开Window->Preferences->General->keys 修改 ...

  6. 前端知识点总结——HTML

    HTML:HTML4.01 指的就是网页技术HTML5:HTML4.01的升级版本 1.web的基础知识 web与Internet1.Internet:全球性的计算机互联网络,因特网,互联网,交互网2 ...

  7. python hdfs初体验

    新建目录 chr 新建文件hdfstest1.txt并写入内容 复制hdfstest1.txt的内容到hdfstest2.txt

  8. Linux之Nginx服务 nfs文件存储 负载均衡

    一.搭建Nginx服务 Nginx 是俄罗斯人编写的十分轻量级的HTTP 服务器,Nginx,它的发音为"engine X",是一个高性能的HTTP和反向代理服务器,同时也是一个I ...

  9. Difference between x:Reference and x:Name

    {x:Reference ...} -> returns just a reference of an object it doesn't create that "bridge&qu ...

  10. Bootstrap历练实例:导航中的表单

    Bootstrap历练实例:导航中的表单,它是使用class.navbar-form类,这确保了表单适当的垂直对齐和在较窄的视口中折叠的行为,使用这个对齐方式选项来决定导航栏中的内容放置在哪里. 实例 ...