UVa133 The Dole Queue(救济金发放)

UVa133 The Dole Queue(救济金发放)_第1张图片

#include
using namespace std;
const int maxn = 25;
int n, k, m, a[maxn];
 
int go(int p, int d, int t){
    while(t--){
        do{
            p = (p + d + n -1)%n + 1;
        }while(a[p] == 0);
    }
    return p;
}
int main(){
    while(cin >> n >> k >> m && n){
        int p1 = n, p2 = 1;
        for(int i = 1; i <= n; i++)a[i] = i;
        int left = n;
        
        while(left){
            p1 = go(p1, 1, k);
            p2 = go(p2, -1, m);
            printf("%3d", p1);
            left--;
            if(p2 != p1){
                printf("%3d", p2);
                left--;
            }
            a[p1] = a[p2] = 0;
            if(left)printf(",");
        }
        printf("\n");
    }
    return 0;
}

 

你可能感兴趣的:(C/C++)