In a serious attempt to downsize (reduce) the dole queue, the new national green labor 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 labor 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 shoshould 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 shocould not be a trailing comma ).
Sample Input
10 4 3
0 0 0
Sample output
4 8, 9 5, 3 1, 2 6, 10, 7
1 # include <iostream> 2 # include <string. h> 3 # include <stdio. h> 4 # include <ctype. h> 5 # include <algorithm> 6 # include <stack> 7 # include <queue> 8 # include <set> 9 # include <math. h> 10 # include <vector> 11 # include <map> 12 # include <deque> 13 # include <list> 14 using namespace STD; 15 # define maxn 2516 int N, k, M, a [maxn]; 17 int go (int p, int D, int t) // construct a walking function, skip position 18 {19 while (t --) 20 {21 do22 {23 p = (p + D + N- 1) % N + 1; 24} 25 while (A [p] = 0); 26} 27 return P; 28} 29 int main () 30 {31 While (scanf ("% d", & N, & K, & M) = 3 & N) 32 {33 for (INT I = 1; I <= N; I ++) 34 A [I] = I; 35 int left = N; 36 int p1 = n, p2 = 1; // initialize the Moving position. Use P1 counterclockwise and p237 while (left) 38 clockwise {39 p1 = go (P1, 1, k ); 40 P2 = go (P2,-1, m); 41 printf ("% d", P1); 42 left --; 43 If (P1! = P2) 44 {45 printf ("% d", P2); 46 left --; 47} 48 if (left) 49 printf (","); 50 A [P1] = A [P2] = 0; 51} 52 printf ("\ n"); 53} 54 return 0; 55}View code