From: http://poj.grids.cn/practice/2746
Time limit:
1000 ms
Memory limit:
65536kb
-
Description
-
Joseph's question: there are n monkeys in a circle clockwise (ranging from 1 to n). The number starts from 1st and ranges from m to m until the monkeys exit the circle, the rest of the monkeys start reporting data from 1. In this way, when there is only one monkey in the circle, this monkey is the Monkey King. After programming and inputting n, m, output the number of the Monkey King.
-
Input
-
Each line is two integers separated by spaces. The first is N, and the second is m (0 <m, n <= 300 ). The last line is:
0 0
-
Output
-
For each line of input data (except the last line), the output data is also a row, that is, the number of the last Monkey King
-
Sample Input
-
6 212 48 30 0
-
Sample output
-
517
# Include <iostream> using namespace STD; # define Max 310 // This is a ring. You cannot use array B to copy array A (this is generally a single item) // Therefore, array Implementation: Skip the M monkey // What is the skipped monkey: feature is 0 // how to find the M monkey in the loop (from 0 to find the M-1) // find the last return condition, return condition // return method: n-1 to 0 // return condition: // The next in the ring is (This + 1) mod N // 1 2 3 4 5 6 // 0 1 0 1 0 1 (I) 3 // 1 0 3 0 5 0 // out: 2 4 6 // 1 0 3 0 5 0 // 0 1 0 // 1 0 0 0 5 0 // out: 3 // 1 0 0 0 5 0 // 0 1 // 1 0 0 0 0 0 0 // out: 5 // 1 0 0 0 0 0 // endint main () {int n = 0; int m = 0; int A [Max] = {0}; while (1) {CIN> N; CIN> m; // 0000000000 // cout <n <m <Endl; If (n = 0) break; // 00000000000000 int nptr = 0; int I = 0; while (I <n) {A [I] = I + 1; // cout <A [I]; I ++ ;}// init for (I = 0; I <n; I ++) {// init // cout <I <Endl; int ncount = 0; while (ncount <m) {While (A [nptr] = 0) nptr = (nptr + 1) % N; ncount ++; nptr = (nptr + 1) % N ;} // nptr = m nptr --; If (nptr <0) nptr = n-1; if (I = n-1) {// For (Int J = 0; j <n; j ++) cout <"J =" <j <"A [J] =" <A [J] <Endl; // cout <"nptr =" <nptr <Endl; cout <A [nptr] <Endl; // cout <A [nptr] <Endl ;} // else {// cout <"I =" <I <Endl; // For (Int J = 0; j <n; j ++) cout <"J =" <I <"A [J] =" <A [J] <Endl; // cout <"nptr =" <nptr <Endl; //} A [nptr] = 0; // nptr = 0 ;}} system ("pause"); Return 0 ;}