133-the Dole Queue
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_ problem&problem=69
In a serious attempt to downsize (reduce) The dole queue, the New national Green Labour Rhinoceros Party has decided on th E following strategy. Every Day All dole applicants is 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'll be standing on 1 ' s left). Starting from 1 and moving counter-clockwise, one Labour official counts off K applicants, while another official starts F Rom 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 (WHO) is sent off to become a politician. Each official then starts counting again in the next available person and the process continues until the are left. Note This two victims (sorry, trainees) leave the ring simultaneously and so it is possible for one official to count a P Erson already selected by the other official.
Input
Write a program of that would successively read in the three numbers (n, K and M K, m > 0, 0 < N < 2 0) and determine the order in which the applicants are sent out for retraining. Each set of three numbers'll is on a separate line and the end of data would 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. Each number should is in a field of 3 characters. For pairs of numbers list the person chosen by the counter-clockwise official. Separate successive pairs (or singletons) by commas (but there, not be a should trailing).
Sample input
4 3
0 0 0
Sample output
4 8, 9 5, 3 1, 2 6, 10, 7
where represents a space.
Complete code:
/*0.015s*/#include <cstdio> #include <cstring> int people[20];
BOOL VIS[20];
int main () {int n, K, M, I, left, Count, Flag1, Flag2;
for (i = 1; i < ++i) people[i] = i;
while (scanf ("%d%d%d", &n, &k, &m), N) {memset (Vis, 0, sizeof (VIS));
left = n;
Flag1 = 0, Flag2 = n-1; while (left) {for (count = 1;; Flag1 = (Flag1 + 1)% n) {if (!vi
S[FLAG1]) {if (Count < K) ++count;
else {printf ("%3d", Flag1 + 1);///I was misled by the asked, PE once ...
--left;
Break
for (count = 1; flag2 = (flag2-1 + N)% n) {
if (!vis[flag2]) { if (Count < m) ++count;
else {if (Flag2!= Flag1) printf ("%3d", Flag2 + 1),--left;
VIS[FLAG1] = Vis[flag2] = true;
Break
}} if (left) Putchar (', ');
} putchar (10);
return 0; }
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/