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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章