Saturday, June 6, 2015

UVa 133 - The Dole Queue

// UVa 133 - The Dole Queue

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

bool first;

int main() {
	int n, k, m;
	while ((cin >> n >> k >> m) && n && k && m) {
		int a = n - 1;
		int b = 0;
		int out = 0;
		first = true;
		bool dead[20];
		memset(dead, false, sizeof(dead));
		// loop
		do {
			// move
			for (int i = 0; i < k; i++)
				do {
					a = (a + 1) % n;
				} while (dead[a]);
			for (int i = 0; i < m; i++)
				do {
					b = (b - 1 + n) % n;
				} while (dead[b]);
			// print
			if (!first)
				printf(",");
			else
				first = false;
			printf("%3d", a + 1);
			dead[a] = true;
			out++;
			if (a != b) {
				printf("%3d", b + 1);
				dead[b] = true;
				out++;
			}
		} while (out < n);
		cout << endl;
	}

	return 0;
}

No comments:

Post a Comment