UVA 133-The Dole Queue
题目大意:n个人编号1~n,输出从1开始顺时针数第k个和从10开始逆时针数第m个,输出的数字删去,直到全部数字都输出后结束,当顺时针和逆时针访问到同一个时只输出一个。
解题思路:用二维数组模拟,表示出左边和右边的数
#include <stdio.h>
#include <iostream>
using namespace std;
int l[25][3]; //0代表左,1代表右, 2代表是否选过
int n, k, m;
int x, y;
void turn(int q, int p) {
int t1 = l[q][0];
int t2 = l[q][1];
if(l[t1][2] == 0)
t1 = l[t1][0];
if(l[t2][2] == 0)
t2 = l[t2][1];
l[t1][1] = t2;
l[t2][0] = t1;
if(p == 1)
y = t1;
else if(p == 2)
x = t2;
}
void find() {
int p1 = x, p2 = y;
for(int i = 1; i < k; i++) {
x = l[x][1];
}
for(int i = 1; i < m; i++) {
y = l[y][0];
}
l[x][2] = 0;
l[y][2] = 0;
if(x == y) {
n--;
printf("%3d", x);
turn(x, 2);
y = l[x][0];
}
else {
n = n - 2;
printf("%3d%3d", x, y);
turn(x, 2);
turn(y, 1);
}
}
int main() {
while(scanf("%d%d%d", &n, &k, &m) && n + k + m != 0) {
for(int i = 1; i <= n; i++) {
l[i][0] = i - 1;
l[i][1] = i + 1;
l[i][2] = 1;
if(i - 1 == 0)
l[i][0] = n;
if(i + 1 > n)
l[i][1] = 1;
}
x = 1, y = n;
while(1) {
find();
if(n == 0) {
printf("\n");
break;
}
printf(",");
}
}
return 0;
}