The Dole Queue 约瑟夫环 模拟

A CM   Conte st Problems  Archive                                          Univers ity of Valladolid (SPAIN) 
133      The    Dole     Queue 
In a serious attempt   to downsize  (reduce) the dole  queue, The  New   National Green   Lab our Rhino  ceros 
Party  has decided  on the following strategy. Every  day all dole applicants will b e placed in a large circle, 
facing inwards.  Someone   is arbitrarily chosen as numb  er 1, and the rest are numb ered counter-clo ckwise 
up  to N  (who  will b e standing on 1's left). Starting from  1 and  moving  counter-clo ckwise,  one lab our 
ocial  counts  o  k applicants, while  another  ocial  starts from  N  and  moves  clo ckwise, counting  m 
applicants.  The two  who  are chosen  are then sent o for retraining; if b oth ocials pick the same p erson 
she (he) is sent o to b ecome  a p olitician. Each ocial then  starts counting  again at the next  available 
p erson and  the pro cess continues until no-one  is left. Note that the  two victims  (sorry, trainees) leave 
the ring simultaneously,   so it is p ossible for one ocial to count a p erson already selected by the other  ocial. 


Input 


Write  a program   that will successively read in  (in that order) the three  numb  ers (N, k and  m; k, m  > 


0, 0 <  N <  20) and  determine  the  order in which  the applicants are  sent o for retraining. Each  set of 


three numb  ers will b e on a separate line and the  end of data will b e signalled by three zero es (0 0 0). 


Output 


For each  triplet, output a single line of numb  ers sp ecifying the order in which p eople are chosen.  Each 


number should  b e in a eld of 3 characters. For pairs of numb ers list the p erson chosen by the counter- 


clo ckwise ocial   rst. Separate  successive pairs (or singletons) by  commas   (but  there should  not b e a 


trailing comma). 


Sample     input 


10  4 3 


0  0 0 


Sample     output 



题目大意:有一个由编号分别为1-n的人组成的圆环,一个人从第n个人开始顺时针数k个人,一个人从第1个人开始逆时针数m个人,输出两个人最终所选择的人的顺序。

思路:约瑟夫环的暴力解法,一个指针顺时针循环,一个指针逆时针循环,数到的人进行标记。

#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX 30
using namespace std;
int peo[MAX],n;


int go(int pos,int dir,int step)
{
    int cnt=0;
    while(step!=cnt)
    {
        pos=(pos+dir+n)%n;
        if(peo[pos]!=0)
            cnt++;
    }
    return pos;

}


int main()
{
    int k,m,i;
    int left,p1,p2;
    while(scanf("%d%d%d",&n,&k,&m)&&n&&k&&m)
    {
        memset(peo,0,sizeof(peo));
        for(i=0;i<n;i++)
            peo[i]=i+1;
        left=n;
        p1=n-1;
        p2=0;
        while(left)
        {
            p1=go(p1,1,k);
            p2=go(p2,-1,m);
            printf("%3d",peo[p1]);
            left--;
            if(p1!=p2)
            {
                printf("%3d",peo[p2]);
                left--;
            }
            if(left)
                printf(",");
            peo[p1]=peo[p2]=0;

        }
        printf("\n");
    }
    return 0;
}







你可能感兴趣的:(Queue,模拟,约瑟夫环,the,Dole)