救济金发放(UVa133)

  题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=69

C++11代码如下:

 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std;
 4 int que[25];
 5 int n, k, m;
 6 
 7 int go(int p, int d, int t) { //d表示步长,值为1表示逆时针,-1表示顺时针
 8     while (t--) {
 9         do {
10             p = (p + d + n - 1) % n + 1;  //防止(p+d)%n==0,(p+d+n-1)相当于旋转一周,最后加1
11         } while (que[p]==0);
12     }
13     return p;
14 }
15 int main() {
16     while ((cin >> n >> k >> m) && n != 0) {
17         for (int i = 1; i <= n; i++) que[i] = i;
18         int left = n;  //剩下的人数
19         int p1 = n, p2 = 1;  //起始位置
20         while (left) {
21             p1 = go(p1, 1, k);
22             p2 = go(p2, -1, m);
23             cout << setw(3) << p1;
24             left--;
25             if (p1 != p2) {
26                 cout << setw(3) << p2;
27                 left--;
28             }
29             que[p1] = que[p2] = 0;
30             if (left) cout << ',';
31         }
32         cout << endl;
33     }
34     return 0;
35 }

 

posted on 2018-07-03 20:23  Pink.Pig  阅读(604)  评论(0编辑  收藏  举报