紫书第五章训练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[100];
int n;
int main()
{

    while(cin>>n&&n)

    {

        int k=1;
        int l=n;
        for(int i=1; i<=n; i++)s[i]=i;
        if(n==1)
        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 0;
}

时间: 2024-10-08 20:50:37

紫书第五章训练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 compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. Input Standard input consists of a number

紫书第五章训练 uva 10763 Foreign Exchange by crq

Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very successful foreign student exchange program. Over the last few years, demand has sky-rocketed and now you need assistance with your task.The

紫书第五章训练 uva 10391 Compound Words by crq

You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. Input Standard input consists of a number of lowercase words

紫书第五章训练 uva 1594 Ducci Sequence by crq

Description A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... , an), the next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers: ( a1, a2, ... , an)  (| a1 - a

紫书第三章训练2 暴力集

A - Master-Mind Hints MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players agree upon the leng

lrj紫书第五章

UVA-1592 1 // UVa1592 Database 2 // Rujia Liu 3 // 本程序只是为了演示STL各种用法,效率较低.实践中一般用C字符串和哈希表来实现. 4 5 #include<iostream> 6 #include<cstdio> 7 #include<vector> 8 #include<string> 9 #include<map> 10 #include<sstream> 11 using n

紫书第四章训练 UVA1589 Xiangqi by 15 周泽玺

Xiangqi is one of the most popular two-player board games in China. The game represents a battle between two armies with the goal of capturing the enemy's "general" piece. In this problem, you are given a situation of later stage in the game. Be

紫书第4章 函数和递归

1  序 系统的整理下第四章的学习笔记.同上次一样,尽量在不依赖书本的情况下自己先把例题做出来.这次有许多道题代码量都比较大,在例题中我都用纯C语言编写,但由于习题的挑战性和复杂度,我最终还是决定在第五章开始前,就用C++来完成习题.不过所有的代码都是能在C++提交下AC的. 在习题中,我都习惯性的构造一个类来求解问题,从我个人角度讲,这会让我的思路清晰不少,希望自己的一些代码风格不会影响读者对解题思路的理解. 其实在第四章前,我就顾虑着是不是真的打算把题目全做了,这些题目代码量这么大,要耗费很

6.12白书第五章图论总结——司雨寒

之前我的图论一直都是DFS一下,BFS一下,求个欧拉回路,拓扑排个序这种渣渣水平. 终于鼓起勇气拾起白书第五章的东西. 学(bei)习(song)了一下求双连通分量,二分图的判定,强连通分量,2-SAT. DFS加上时间戳这个东西,很强大. 最后刷了白书上的例题: BCC: LA3523 可以参加会议的是双联通分量上的奇圈 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include