标签:
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