Title Description
In order to shorten the relief team, NNGLRP decided the following strategy: every day to apply for relief products will be placed in a large circle, facing inside. Select an individual number 1th, and the others start with a counter-clockwise number until N. An official starts counterclockwise, counts K applicants, and then another official, Nth, starts a number of m applicants in a clockwise direction, and these two people are sent to re-education. If the two officials are the same person, the person is sent to politics, and then 2 officials continue to choose among the remainder until no one is left, and notice that two of the chosen are gone at the same time, so it is possible that two officials have chosen a single person.
Input
Input contains multiple sets of test data, each set of test data contains three numbers n,k and M (K, M > 0,0<n<20). When the input is 0 0 0 means the input ends.
Output
Output a column for each set of test data. Outputs the number order of the selected requester (a pair of pairs). The width of each number is 3. Each of the front-numbered officials elected by an officer with a clockwise number in the back (but if the 2 officials elect the same person, there will only be one number). Each pair is separated by commas. Please refer to the sample Output for format.
Sample Input
13 17 42
Sample Output
1 3, 5 7, 2 4, 6
#include <iostream>int N, K, M, a[25];int go (int p, int d, int t) {while (t--) {do {p = (p + D + n-1)% n + 1;
//Important! } while (a[p] = = 0);} return p;} int main () {using namespace Std;while ((cin>>n>>m>>k) && (n&&k&&m)) {for (int i = 1; I <= N; i++) A[i] = I;int left = n; count remaining int p1 = n, p2 = 1;while (left) {P1 = Go (P1, 1, k); Counterclockwise P2 = Go (P2,-1, m); Clockwise cout<<a[p1]; left--; if (P2! = p1) {cout<< " " <<a[p2]; left--;} A[P1] = a[p2] = 0; Assigned value 0if (left) cout<< ",";} Cout<<endl;} return 0;}
Extract:
As for the next position why is P = (p + n + d)% n. It's really simple. Because we are walking step by step, there are only two kinds of boundary conditions. Assuming the current position is p (0 = <p<n),
The first boundary: P + 1 > n-1, that is, p + 1 should be at this time to reach 0 position, but at this time p + 1 = n, if we take the remainder, then (P + 1)% T = 0, t = N (t indicates the cycle size of the circle).
is exactly the same, and because t = N, so (P + t + 1)% T is still unchanged.
The second boundary: P-1 < 0, that is, p-1 at this time the value is-1, for this case can be reversed, it is backward after 1 units, can be seen as a forward walk T-1 units that is p-1 equivalent to P + T-1
, we have to wait for the position at this time, and then to the remainder, (P + T-1)% T.
Uva133--the Dole Queue