1. 程式人生 > >例題4-3 救濟金髮放 UVa133

例題4-3 救濟金髮放 UVa133

int book[100];//領過的標記1,沒領過的標記0
int main(){
    int n,k,m;
    int kcount,mcount;
    int ki,mi;
    int kout,mout;
    int first;
    int ncount;
    while(scanf("%d%d%d",&n,&k,&m)==3&&n&&k&&m){
        memset(book,0,sizeof(book));
        ncount=0;
        ki=0;
        mi=n+1;
        first=1;
        while(ncount!=n){//n個人全被處理完畢//處理手法有些類似快速排序
            kcount=0;
            mcount=0;
            while(kcount!=k){//k系列處理
                ki++;
                if(ki>n)//ki越界處理
                    ki=1;
                if(book[ki]==0)//未被選中計數  ki為當前值
                    kcount++;
               
            }
            while(mcount!=m){//m系列處理
                mi--;
                if(mi<1)//mi越界處理
                    mi=n;
                if(book[mi]==0)//未被選中計數 mi為當前值
                    mcount++;
            }
            book[ki]=1;//不用擔心ki==mi
            book[mi]=1;
           
            if(first){//列印處理
                first=0;
                if(ki!=mi){
                    printf("%3d%3d",ki,mi);
                    ncount+=2;
                }
                else{
                    printf("%3d",ki);
                    ncount++;
                }
            }else{
                if(ki!=mi){
                    printf(",%3d%3d",ki,mi);
                    ncount+=2;
                }
                else{
                    printf(",%3d",ki);
                    ncount++;
                }
            }
        }
        printf("\n");
    }
    return 0;
}