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个数,按顺序排列。
1.从顶部取出一张牌;
2.将下一张放到最后;
3.重复1.2步骤,直到剩余最后一张牌;
解题思路:申请一个vector数组,从前面输出一个数字后,用v.push_back();
将下一个数复制到数组末尾,且数组长度加一。在n-2之前输出数后都
要紧跟输出“, ”;最后换行输出剩下的一个数;
注意事项:1.在n!=1;时“:”后面存在一个空格;
2.在n=1时Discarded cards:后没有空格Remaining card:后有空格;
#include<vector>
#include<iostream>
using namespace std;
int main()
{
vector<int> v;
int m,n,i,j,k;
while(scanf("%d",&n),n!=0)
{
vector<int> v;
for(i=0;i<n;i++)
v.push_back(i+1);
if(n==1)
cout<<"Discarded cards:";
else
cout<<"Discarded cards: ";
vector<int>::iterator it;
/* it=v.begin();
while(it!=v.end())
{
cout<<*it<<",";
it++;
}*/
for(i=0;i<n-1;i++)
{
cout<<v[i];
i++;
v.push_back(v[i]);
n++;
if(i<n-2)
cout<<", ";
}
cout<<endl;
cout<<"Remaining card: ";
cout<<v[n-1]<<endl;
}
return 0;
}