UVa 10935卡片游戏

很简单的一个题目,就是队列的运用就可以了,就是注意一下1的时候的情况就可以了。

 1 #include<iostream>
 2 #include<queue>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     queue<int> s;
 8     int n,t;
 9     while (cin >> n && n)
10     {
11         for (int i = 1; i <= n; i++)
12         {
13             s.push(i);
14         }
15         int flag = 1;
16         if(n>1) cout << "Discarded cards: ";
17         else    cout << "Discarded cards:" << endl;
18         while (n > 1)
19         {
20             if (flag == 1)
21             {
22                 t = s.front();
23                 s.pop();
24                 cout << t;
25                 if (n > 2) cout << ", ";
26                 else cout << endl;
27                 flag = 0;
28                 n--;
29             }
30             else
31             {
32                 t = s.front();
33                 s.pop();
34                 s.push(t);
35                 flag = 1;
36             }
37         }
38         cout << "Remaining card: " << s.front() << endl;
39         s.pop();
40     }
41     return 0;
42 }

2016-11-23 22:55:23

原文地址:https://www.cnblogs.com/zyb993963526/p/6095919.html