一:题目

给定n张卡片,按照1-n的顺序编号,然后拿出一张卡片扔掉,拿出一张卡片放到最后,重复该操作直到只剩1张卡片。

求扔掉的卡片序列和最后剩的卡片的编号。

(一)样例输入

7        //卡牌编号从1到7
19       //卡牌编号从1到19

(二)样例输出

Discarded cards: , , , , ,
Remaining card:
Discarded cards: , , , , , , , , , , , , , , , , ,
Remaining card:
Discarded cards: , , , , , , , ,
Remaining card:
Discarded cards: , , , ,
Remaining card:

二:代码实现

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <queue> using namespace std; #define MAX_N 15 int main103()
{
freopen("data5_3_h.in", "r", stdin);
freopen("data5_3_h.out", "w", stdout); int num,first_ele;
while (cin>>num&&num!=)
{
queue<int> card;  //使用队列进行模拟
bool flag = false;
for (int i = ; i <= num; i++)
card.push(i);
cout << "Discarded cards: ";
while (card.size()!=)
{
if (!flag)
{
cout << card.front();
if (card.size() != )
cout << ", ";
}
else
card.push(card.front());
card.pop();
flag = !flag;
}
cout << "\nRemaining card:" << card.front() << endl;
} freopen("CON", "r", stdin);
freopen("CON", "w", stdout);
return ;
}
05-21 20:48