问题描述
p10935原题链接:UVaOJ 10935 - Throwing cards away I
相关说明:本题为《算法竞赛入门经典(第2版)》习题 5-3
解法一:队列模拟
直接用 queue 模拟抽牌放牌操作,注意 n=1 时 Discarded cards: 后没有空格,不然要 PE.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n && n) { queue<int> q; for (int i = 1; i <= n; i++) q.push(i); cout << "Discarded cards:"; while (q.size() > 1) { cout << " " << q.front() << (q.size() == 2 ? "" : ","); q.pop(); q.push(q.front()); q.pop(); } cout << endl << "Remaining card: " << q.front() << endl; } return 0; } |
1 |
#TODO |