码迷,mamicode.com
首页 > Windows程序 > 详细

Throwing cards away I

时间:2015-07-16 23:59:39      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:


                                                                           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"
using namespace std;
const int maxn=2600;
int a[maxn];
int n;

void Work()
{
for(int i=1;i<=n;i++)
a[i]=i;
int head=1;
int tail=n;
if(tail==1)
cout<<endl<<"Remaining card: "<<a[head]<<endl;
else
{
cout<<‘ ‘;
while(head<tail)
{
cout<<a[head++];
if(head<tail) cout<<", ";
a[++tail]=a[head++];
if(head==tail) {cout<<endl<<"Remaining card: "<<a[head]<<endl;}
}
}
}


int main()
{
while(cin>>n&&n)
{
	cout<<"Discarded cards:";
Work();
}
return 0;
}

 

Throwing cards away I

标签:

原文地址:http://www.cnblogs.com/zsyacm666666/p/4652588.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!