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

 
这题目的意思就是把把一张牌拿出来,第二张牌放到最后,如此循环直到还剩下最后一个张牌。
依次输出拿出来的牌
在输出剩下的一张牌
 
 
 
 
代码如下:(新手参照书上的方法写的,望各位海涵)
 
 

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int n;
while(cin>>n&&n)
{
int a[100],b[100],c[1];
int i,j=0,head,tail;
head=0; //将head标记到队列开头
tail=n; //将tail标记到队列最后一位的后一位,因为需要留一个位置给开头的数
for(i=0; i<n; i++)
a[i]=i+1;// 1 2 3 4 5 6 7
if(n==1)
{
cout<<"Discarded cards:"<<endl;
cout<<"Remaining card: 1"<<endl;
}

else
{
while(head+1<tail) //这里的判断条件表示当a数组还没有到一个数时,就循环下去
{
b[j++]=a[head]; //将第一个数赋给数组b,并且j由0开始增加
head++; //head向后移动一位,此时去掉了”第一个数“ 2 3 4 5 6 7
a[tail]=a[head]; //将第二个数移动到队列最后 2 3 4 5 6 7 2
c[0]=a[tail]; //每次将最后一个数赋给长度为1的数组c,当还剩最后一个数时,c[0]就是剩下的数
tail++; //tail向后移动一位,为下一个移动到队尾的数留位子
head++; //因为已经将队列的第一个移动到了队尾,所以head向后移动一位 3 4 5 6 7 2
}
cout<<"Discarded cards: ";
for(int k=0; k<=j-2; k++) //因为j++。当跳出while时,j还多加了1,所以多减个1
cout<<b[k]<<", ";
cout<<b[j-1]<<endl; //与上一条一样
cout<<"Remaining card: ";
cout<<c[0]<<endl;
}

}

}

特别注意输出是否少多空格,符号问题,反正我是吃了苦头.....

Throwing cards away I的更多相关文章

  1. UVa 10935 - Throwing cards away I (队列问题)

    原题 Throwing cards away I   Given is an ordered deck of n cards numbered 1 to n with card 1 at the to ...

  2. 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 ...

  3. [刷题]算法竞赛入门经典(第2版) 5-3/UVa10935 - Throwing cards away I

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa10935 - Throwing cards away I #incl ...

  4. Throwing cards away I uva1594

     Throwing cards away I Given is an ordered deck of  n  cards numbered 1 to n  with card 1 at the t ...

  5. 紫书第五章训练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 ...

  6. UVA10940 - Throwing cards away II(找到规律)

    UVA10940 - Throwing cards away II(找规律) 题目链接 题目大意:桌上有n张牌,依照1-n的顺序从上到下,每次进行将第一张牌丢掉,然后把第二张放到这叠牌的最后.重复进行 ...

  7. UVa 10935 (水题) Throwing cards away I

    直接用STL里的queue模拟即可. #include <cstdio> #include <queue> using namespace std; ; int discard ...

  8. UVa 10935 Throwing cards away I【队列】

    题意:给出 n张牌,从上往下编号依次为1到n,当牌的数目至少还剩下2张时,把第一张牌扔掉,然后把新的一张牌放在牌堆的最底部,问最后剩下的那一张牌是哪一张牌. 模拟队列的操作------- #inclu ...

  9. uva 10935 throwing cards away <queue>

    Given is an ordered deck of    n    cards numbered 1 to    n    with card 1 at the top and card    n ...

随机推荐

  1. js常用代码收藏

    --1.遍历string分割为数组 <script language="javascript"> str="2,2,3,5,6,6"; //这是一字 ...

  2. javascript一些常用操作

    一:验证日期 1:日期必须满足yyyy-MM-dd格式 2:日期必须是合法的日期,如2016-02-30就是不存在 //验证就诊日期 function checkVisitDate(date){ va ...

  3. HTTP重定向服务器

    程序基本流程如下: 代码组织结构如下: HTTP重定向服务主线程: package com.server; import java.io.IOException; import java.net.Se ...

  4. JDBC的批量批量插入

    本文部分转载于:http://blog.itpub.net/29254281/viewspace-1151785/ http://www.cnblogs.com/chenjianjx/archive/ ...

  5. [改善Java代码]由点及面,一叶知秋----集合大家族

    Java中的集合类实在是太丰富了,有常用的ArrayList.HashMap,也有不常用的Stack. Queue,有线程安全的Vector.HashTable,也有线程不安全的LinkedList. ...

  6. hdu 3440 差分约束

    看完题目第一遍,感觉很简单.当写完程序跑测试用例的时候,发现第二个总是过不了,然后好好研究了一下测试用例,才知道原来不是程序有问题,而是我的建图方式错了.对于这些无序的点,如果高的在右边,不等式是di ...

  7. 从零单排Linux – 1 – 简单命令

    从零单排Linux – 1 – 简单命令 Posted in: Linux 从零单排Linux – 1 一.Linux的简单命令: 1.忘记root密码: 读秒时按任意键进入 – e – ↓选择第二个 ...

  8. Java Concurrency - wait & notify, 等待通知机制

    生产者消费者问题是一个常见的多线程同步案例:一组生产者线程和一组消费者线程共享一个初始状态为空.大小为 N 的缓冲区.只有当缓冲区没满的时候,生产者才能把消息放入缓冲区,否则必须等待:只有缓冲区不空的 ...

  9. HDOJ2021发工资咯:)

    发工资咯:) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  10. HDOJ2016数据的交换输出

    数据的交换输出 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...