UVA-10935 Throwing cards away I
题目: Throwing cards away I
Time limit: 3.000 seconds
代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #include <iostream> #include <algorithm> #include <sstream> #include <string> #include <numeric> #include <cstring> #include <map> #include <set> #include <utility> #include <map> #include <list> #include <queue> #include <functional> #include <bitset> #include <stack> using namespace std;int card[100000 ];int main () { int N; while (cin >> N &&N) { queue<int > Q; for (int i = 1 ;i <= N;++i) { Q.push (i); } int end = 0 ; while (Q.size () > 1 ) { card[end++] = Q.front (); Q.pop (); Q.push (Q.front ()); Q.pop (); } if (N == 1 ) { cout << "Discarded cards:" << endl; } else { cout << "Discarded cards: " << card[0 ]; for (int i = 1 ;i < end;++i) cout << ", " << card[i]; cout << endl; } cout << "Remaining card: " << Q.front () << endl; } }
解析&吐槽: 简单的队列题,模拟一下即可。需要注意的是输出的结果在行末不能有多余的空格,如果题目的输入只有一张卡的话, 输出的第一行在冒号后面应该紧跟换行。我的第一个 wa 就是为了在冒号后面加了空格方便输入,结果没有可丢弃的卡 的时候就多出空格来了。
本作品采用
署名-相同方式共享 4.0 国际
进行许可。欢迎转载、使用、重新发布,但务必保留文章署名
“不科学的科学君” (Liu233w) 与博客链接:
https://liu233w.github.io
,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请
与我联系
。