题目大意:N个数组成的环,然后分别逆时针的走K个,顺时针的走m个,选中的数就剔除,不在下次走的范围内。打印出提出的顺序。
解题思路:用数组摸拟。每次走都 p2= (p2 - 1 + n) % n,防止 减到负数。
#include<stdio.h>
#include<string.h>
const int N = 25;
int n, k, m;
bool s[N];
int main() {
while(scanf("%d%d%d", &n, &k, &m) ,( n || m || k)) {
int p1 = -1, p2 = n, i, count = n;
memset(s, 1, sizeof(s));
while(count) {
for( i = 1; i <= k; ) {
p1 = (p1 + 1 + n)% n ;
if(s[p1])
i++;
}
for(i = 1; i<= m; ) {
p2 = (p2 - 1 + n) % n ;
if(s[p2])
i++;
}
if(p1 == p2) {
count --;
s[p1] = 0;
if(count)
printf("%3d,", p1 + 1);
else
printf("%3d\n", p1 + 1);
}
else {
count -= 2;
s[p1] = s[p2] = 0;
if(count)
printf("%3d%3d,", p1 + 1, p2 + 1);
else
printf("%3d%3d\n", p1 + 1, p2 + 1);
}
}
}
return 0;
}