The Dole Queue

UVa133

相当于约瑟夫环问题,只是可以写个函数即可顺时针走,又可以逆时针走。

#include <stdio.h>
#include <string.h>
const int maxn = 25;
int vis[maxn], n;
int go ( int start, int d, int step )
{
    for ( int i = 0; i < step; i ++ )
    {
        do
        {
            start = ( start+d+n )%n;
        }while ( vis[start] );
    }
    return start;
}
int main ( )
{
    int m, k, t;
    while ( ~ scanf ( "%d%d%d", &n, &k, &m ) && ( n || m || k ) )
    {
        memset ( vis, 0, sizeof ( vis ) );
        t = n;
        int p, q;
        p = n-1;
        q = 0;  //注意p是从最后一个,q是第一个
        while ( t )
        {
            p = go ( p, 1, k ); //顺时针走k
            q = go ( q, -1, m ); //逆时针走m
            printf ( "%3d", p+1 );  //要加1
            t --;
            if ( p != q )
            {
                printf ( "%3d", q+1 );
                t --;
            }
            vis[q] = vis[p] = 1;    //标记
            if ( t )    //最后没有,
                printf ( "," );
        }
        printf ( "\n" );
    }
    return 0;
}


你可能感兴趣的:(The Dole Queue)