思路 ①这里和刽子手游戏有点不同,这里不是一口气,而是需要一步一步走,每走一步就要判断一次,这个数是否被输出过;若输出过,则需要跳过,继续数下一个
②与数据结构中的循环队列结构相似。重点:如何让数字循环回来;如何判断数已经被输出
#include <stdio.h>
#define maxn 25
int a[maxn];
int n, k, m;
int go(int,int,int);
int main()
{
while(scanf("%d%d%d", &n,&k,&m) == 3 && n)
{
for(int i = 1; i <= n; i++)
a[i] = i;
int left = n;
int p1 = 1, p2 = n;
while(left)
{
p1 = go(p1,1,k);
p2 = go(p2,-1,m);
printf("%3d",p1);
left--;
if(p1 != p2)
{
printf("%3d", p2);
left--;
}
a[p1] = a[p2] = 0;
if(left)
printf(",");
}
printf("\n");
}
return 0;
}
int go(int p, int d, int t)
{
while(t--)
{
do
{
p = (p+d+n-1)%n + 1;
}while(a[p] == 0);
}
return p;
}
go函数将两个函数合并在一起了。本来是(p+n+1)%n(向前走,逆时针)和(p+n-1)%n (向后走,顺时针)