0
点赞
收藏
分享

微信扫一扫

python——条件语句

UVA10935 Throwing cards away I 卡片游戏 解题报告

题目链接

https://vjudge.net/problem/UVA-10935

题目大意

桌上有n(n≤50)张牌,从第一张牌(即位于顶面的牌)开始,从上往下依次编号为1~n。当至少还剩下两张牌时进行以下操作:把第一张牌扔掉,然后把新的第一张牌放到整叠牌的最后。输入每行包含一个n,输出每次扔掉的牌以及最后剩下的牌。

解题思路

题目怎么说你就怎么做,非常直球,很显然是维护一个队列。

代码

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e3 + 10;
const int INF = 0x3fffffff;
const int mod = 1000000007;

void solve() {
    int n;
    while (cin >> n, n) {
        queue<int> q;
        for (int i = 1; i <= n; i++) {
            q.push(i);
        }
        cout << "Discarded cards:";
        bool first = true;
        while (q.size() >= 2) {
            if (!first)
                cout << ",";
            first = false;
            cout << " " << q.front();
            q.pop();
            q.push(q.front());
            q.pop();
        }
        cout << "\nRemaining card: " << q.front() << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(18);

    solve();
    return 0;
}
举报

相关推荐

0 条评论