(1.1.7)UVA 10935 Throwing cards away I(直叙式模拟)

#include <iostream>
#include <queue>
#include <cstdio>

using namespace std;

int main() {
	int n;
	const int maxn = 55;
	int ans[maxn];
	while (scanf("%d", &n) != EOF, n) {
		int i;

		queue<int> q;
		for (i = 1; i <= n; ++i) {
			q.push(i);
		}

		int k = 0;

		while (!q.empty()) {
			ans[k++] = q.front();
			q.pop();

			if (!q.empty()) {
				int t = q.front();
				q.pop();
				q.push(t);
			}else{
				break;
			}
		}

		printf("Discarded cards:");
		for (i = 0; i < n - 1; ++i) {
			printf(i < n - 2 ? " %d," : " %d", ans[i], ans[i]);

		}

		printf("\nRemaining card: %d\n", ans[n - 1]);
	}

	return 0;
}

你可能感兴趣的:((1.1.7)UVA 10935 Throwing cards away I(直叙式模拟))