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

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;
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值