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;
}
相关推荐
橙几7 分钟前
击败了90%的解法?Two Sum 从 O(n²) 到 O(n) 的优化之路
算法
叶子爱分享21 分钟前
经典排序算法之归并排序(Merge Sort)
算法·排序算法
珹洺27 分钟前
C++算法竞赛篇:DevC++ 如何进行debug调试
java·c++·算法
呆呆的小鳄鱼1 小时前
leetcode:冗余连接 II[并查集检查环][节点入度]
算法·leetcode·职场和发展
墨染点香1 小时前
LeetCode Hot100【6. Z 字形变换】
java·算法·leetcode
沧澜sincerely1 小时前
排序【各种题型+对应LeetCode习题练习】
算法·leetcode·排序算法
CQ_07121 小时前
自学力扣:最长连续序列
数据结构·算法·leetcode
弥彦_1 小时前
cf1925B&C
数据结构·算法
YuTaoShao2 小时前
【LeetCode 热题 100】994. 腐烂的橘子——BFS
java·linux·算法·leetcode·宽度优先
Wendy14419 小时前
【线性回归(最小二乘法MSE)】——机器学习
算法·机器学习·线性回归