Uva 133:The Dole Queue

题目传送门:Uva 133:The Dole Queue

#include <stdio.h>
int n, k, m;
int a[25];

int go(int position, int direction, int step) // direction为方向
{
    int ct = 0;
    while (true)
    {
        if (a[position] != 0)
        {
            ++ct; // 不为0,计数器加1
            if (ct == step) break; // 找到step个数后停止
        }
        position += direction;
        if (position > n) position = 1;
        if (position < 1) position = n;
    }
    return position;
}

int main()
{
    while (3 == scanf("%d%d%d", &n, &k, &m) && n)
    {
        for (int i = 1; i <= n; ++i)
            a[i] = i;

        int left = n, p1 = 1, p2 = n;

        while (left > 0)
        {
            p1 = go(p1, 1, k);
            p2 = go(p2, -1, m);
            printf("%3d", a[p1]); // 先输出一个
            --left;
            if (p1 != p2) // 如果p1!=p2再输出另一个
            {
                printf("%3d", a[p2]);
                --left;
            }
            a[p1] = a[p2] = 0;
            if (left) // 如果还有剩余,则输出一个逗号
                printf(",");
        }
        putchar('\n');
    }
    return 0;
}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页