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;
}

}

}

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

最新文章

  1. 一个virtualbox开机即aborted的问题解决
  2. js①
  3. Slave2: no datanode to stop(权限)
  4. Linux TTY框架【转】
  5. Redis简单使用方法说明
  6. iOS学习之界面通信
  7. 在Apache+php中使用json来通讯
  8. javascript代码混淆原理
  9. jQuery css,position,offset,scrollTop,scrollLeft用法
  10. ionic3 在windows环境下打包android 正式签名版APK
  11. jvm 年轻代、年老代、永久代
  12. 解决strcmp的错误以及VS的快捷键
  13. SANS社区邮件情报收集【2018-12-4到2019-1-19】
  14. ES系列十一、ES的index、store、_source、copy_to和all的区别
  15. (转)stm32硬件IIC
  16. http4e eclipse plugin 插件介绍
  17. C# 使用printDocument1.Print打印时不显示 正在打印对话框
  18. Docker background
  19. django遇到的坑
  20. 使用 UITabBar 时,子画面虽然已经占满全屏幕,但在其底部,仍然有点击事件(可以响应UITabBar)

热门文章

  1. Android(java)学习笔记107-0:通过反射获得构造方法并且使用
  2. Mysql 5.6 新特性(转载)
  3. hdu 1423 最长公共递增子序列
  4. HTTP - Cookie 机制
  5. Git CMD - rm: Remove files from the working tree and from the index
  6. Memcached学习(二)
  7. iOS app应用界面加载卡顿的问题
  8. OC7_单词个数
  9. BT之下拉菜单
  10. jQuery中的getter和setter方法