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

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

题目链接

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

题目大意

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

解题思路

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

代码

cpp 复制代码
#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;
}
相关推荐
ZhengEnCi2 小时前
S10-蓝桥杯 17822 乐乐的积木塔
算法
贾斯汀玛尔斯2 小时前
每天学一个算法--拓扑排序(Topological Sort)
算法·深度优先
大龄程序员狗哥2 小时前
第25篇:Q-Learning算法解析——强化学习中的经典“价值”学习(原理解析)
人工智能·学习·算法
exp_add32 小时前
质数相关知识
算法
小辉同志3 小时前
215. 数组中的第K个最大元素
数据结构·算法·leetcode··快速选择
小O的算法实验室3 小时前
2025年IEEE TITS,基于矩阵的进化计算+面向无线传感器网络数据收集无人机路径规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
OidEncoder3 小时前
编码器分辨率与机械精度的关系
人工智能·算法·机器人·自动化
memcpy03 小时前
LeetCode 2615. 等值距离和【相同元素分组+前缀和;考虑距离和的增量】中等
算法·leetcode·职场和发展
炽烈小老头4 小时前
【 每天学习一点算法 2026/04/22】四数相加 II
学习·算法
alphaTao4 小时前
LeetCode 每日一题 2026/4/20-2026/4/26
算法·leetcode·职场和发展