1. 程式人生 > >[UVa 133]The Dole Queue 救濟金發放

[UVa 133]The Dole Queue 救濟金發放

pos 逆時針 ica order first triple select mas data

In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros
Party has decided on the following strategy. Every day all dole applicants will be placed in a large
circle, facing inwards. Someone is arbitrarily chosen as number 1, and the rest are numbered counterclockwise

up to N (who will be standing on 1’s left). Starting from 1 and moving counter-clockwise,
one labour official counts off k applicants, while another official starts from N and moves clockwise,
counting m applicants. The two who are chosen are then sent off for retraining; if both officials pick
the same person she (he) is sent off to become a politician. Each official then starts counting again
at the next available person and the process continues until no-one is left. Note that the two victims
(sorry, trainees) leave the ring simultaneously, so it is possible for one official to count a person already
selected by the other official.
Input
Write a program that will successively read in (in that order) the three numbers (N, k and m; k, m > 0,
0 < N < 20) and determine the order in which the applicants are sent off for retraining. Each set of
three numbers will be on a separate line and the end of data will be signalled by three zeroes (0 0 0).
Output
For each triplet, output a single line of numbers specifying the order in which people are chosen. Each
number should be in a field of 3 characters. For pairs of numbers list the person chosen by the counterclockwise
official first. Separate successive pairs (or singletons) by commas (but there should not be a
trailing comma).
Note: The symbol ? in the Sample Output below represents a space.
Sample Input
10 4 3
0 0 0
Sample Output
??4??8,??9??5,??3??1,??2??6,?10,??7

題意:兩個人從一個數字圍成的圈中選數,一個順時針選,另一個逆時針選,各自數一個固定的數後將那個數拿出去,如果兩個人選到了同一個數,就只輸出一個,否則先輸出A的再輸出B的。

題解:用一個大小為人數的數組表示這個圈,如果寫數組元素的移動,代碼就會變得很"醜陋"(個人這樣認為),因此我們將選出去的數賦值為0,數數的時候跳過0去選。

代碼

#include<bits/stdc++.h>
using namespace std;
int n,k,m;
int a[21];
int read()//快速讀入 
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int go(int p,int d,int t)//選人,d是標識是正循環還是逆循環 
{
    while(t--)
        do{p=(p+d+n-1)%n+1;}while(!a[p]);//找到下一個非0的數字 
    return p;
}
int main()
{
    while(1)
    {
        n=read();k=read();m=read();
        if(!n)break;
        for(int i=1;i<=n;i++)a[i]=i;
        int left=n,p1=n,p2=1;
        while(left)
        {
            p1=go(p1,1,k);//正循環 
            p2=go(p2,-1,m);//逆循環 
            printf("%3d",p1);left--;//選出去一個人後,記得減去1 
            if(p2!=p1){printf("%3d",p2);left--;}
            a[p1]=a[p2]=0;//將選出去的數字賦值成0,接下來的循環遇到了就跳過 
            if(left)printf(",");
        }
        puts("");
    }
    return 0;
}

[UVa 133]The Dole Queue 救濟金發放