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. MQTT进阶篇

            我们介绍了最流行的物联网协议MQTT的背景以及基本使用方法.在这篇文章中,我们会继续考察MQTT的高级玩法——与网页应用的交互.MQTT是基于TCP协议实现,基于HTTP的网页应用便无 ...

  2. I/O————字节流

    InputStream字节输入流 OutputStream字节输出流 用于以字节的形式读取和写入数据 下面是使用 字节输入流读取文件字节输出流写入文件 文件可能不存在,所以使用try catch pu ...

  3. JS 操作内容 操作元素

    操作内容:普通元素.innerHTML = "值": 会把标记执行渲染普通元素.innerText = "值": 将值原封不动的展示出来,即使里面有标记 var ...

  4. 5 Options for Distributing Your iOS App to a Limited Audience

    http://mobiledan.net/2012/03/02/5-options-for-distributing-ios-apps-to-a-limited-audience-legally/ I ...

  5. Android Studio 中安装 apk 被拆分成多个 slice,如何禁止?

    Android Studio 3.0.1 中,Run 'app' 时,生成的 apk 被分割成多个 slice: $ adb install-multiple -r D:\...\app\build\ ...

  6. shell脚本,创建50个文件,删除50个文件。

    [root@localhost ~]# cat create50.sh #!/bin/bash #创建50个文件 ` do touch student$i done echo "创建50个文 ...

  7. java在线聊天项目0.9版 实现把服务端接收到的信息返回给每一个客户端窗口中显示功能之客户端接收

    客户端要不断接收服务端发来的信息 与服务端不断接收客户端发来信息相同,使用线程的方法,在线程中循环接收 客户端修改后代码如下: package com.swift; import java.awt.B ...

  8. Luogu P3727 曼哈顿计划E 点分治+hash

    题目: P3727曼哈顿计划E 分析: 大长题面容易给人一种不可做的错觉,但是这题考的知识点都是我们熟悉的. 稍加分析我们可以得到,我们可以把每个点当成一个单独的游戏,如果k=1,就是简单的nim游戏 ...

  9. LeetCode 买卖股票的最佳时机

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...

  10. 【树形dp】7.14城市

    很典型的按照边考虑贡献的题. 题目描述 小A居住的城市可以认为由n个街区组成.街区从1到n依次标号街区与街区之间由街道相连,每个街区都可以通过若干条街道到达任意一个街区,共有n-1条街道.其中标号为i ...