UVa133.The Dole Queue

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=69

13874119 133 The Dole Queue Accepted C++ 0.009 2014-07-13 02:44:49

 The Dole Queue 

In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is arbitrarily chosen as number 1, and the rest are numbered counter-clockwise up to N (who will be standing on 1's left). Starting from 1 and moving counter-clockwise, one labour official counts off k applicants, while another official starts from N and moves clockwise, counting m applicants. The two who are chosen are then sent off for retraining; if both officials pick the same person she (he) is sent off to become a politician. Each official then starts counting again at the next available person and the process continues until no-one is left. Note that the two victims (sorry, trainees) leave the ring simultaneously, so it is possible for one official to count a person already selected by the other official.

Input

Write a program that will successively read in (in that order) the three numbers (N, k and m; k, m > 0, 0 < N < 20) and determine the order in which the applicants are sent off for retraining. Each set of three numbers will be on a separate line and the end of data will be signalled by three zeroes (0 0 0).

Output

For each triplet, output a single line of numbers specifying the order in which people are chosen. Each number should be in a field of 3 characters. For pairs of numbers list the person chosen by the counter-clockwise official first. Separate successive pairs (or singletons) by commas (but there should not be a trailing comma).

Sample input

10 4 3
0 0 0

Sample output

tex2html_wrap_inline34 4 tex2html_wrap_inline34 8, tex2html_wrap_inline34 9 tex2html_wrap_inline34 5, tex2html_wrap_inline34 3 tex2html_wrap_inline34 1, tex2html_wrap_inline34 2 tex2html_wrap_inline34 6, tex2html_wrap_inline50 10, tex2html_wrap_inline34 7

where tex2html_wrap_inline50 represents a space.


解题思路:一道非常类似约瑟夫问题的题目。http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1197
     (关于约瑟夫问题,可以翻阅《具体数学》第一章引例。)

     直接数组模拟就好,没特殊机巧。白书上标程的写法更加精炼,要多学学优化自身的代码!

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 const int maxn = 25;
 8 int n, m, k, peo[maxn];
 9 
10 int solve_z(int cur_first, int step_time) {
11     int ans = cur_first;
12     while (step_time --) {
13         ans ++;
14         if (ans > n) ans = 1;
15         while (peo[ans] == -1) {
16             ans ++;
17             if (ans > n) ans = 1;
18         }
19     }
20     return ans;
21 }
22 
23 int solve_f(int cur_first, int step_time) {
24     int ans = cur_first;
25     while (step_time --) {
26         ans --;
27         if (ans == 0)  ans = n;
28         while (peo[ans] == -1) {
29             ans --;
30             if (!ans) ans = n;
31         }
32     }
33     return ans;
34 }
35 
36 int main() {
37     while (cin >> n >> k >> m) {
38         if (n + k + m == 0) break;
39         for (int i =  0;  i <= n; i ++) {
40             peo[i] = i;
41         }
42 
43         int left_peo = n;
44         int cur1 = n, cur2 = 1;
45         while (left_peo != 0) {
46             cur1 = solve_z(cur1, k);
47             //cout << cur1 << endl;
48             cur2 = solve_f(cur2, m);
49             //cout << cur2 << endl;
50             //system("pause");
51             printf("%3d", cur1); left_peo --;
52             if (cur2 != cur1) {
53                 printf("%3d", cur2);
54                 left_peo --;
55             }
56             //cout << ",";
57             peo[cur1] = peo[cur2] = -1;
58             if(left_peo) cout << ",";
59         }
60         cout << endl;
61     }
62 
63     return 0;
64 }

原文地址:https://www.cnblogs.com/Destiny-Gem/p/3840867.html