UVA 133 the Dole Queue

Source: Internet
Author: User

Original title:
In a serious attempt to downsize (reduce) The dole queue, the New national Green Labour Rhino Ceros Party had decided on the following strategy. Every day all dole applicants'll be placed in a large circle, facing inwards. Someone is arbitrarily chosen as number 1, and the rest was numbered counter-clockwise up to N (who'll be standing on 1 ' s left). Starting from 1 and moving counter-clockwise, one Labour official counts off K applicants, while another official starts F Rom N and moves clockwise, counting m applicants. The Who is chosen is 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 on the next available person and the process continues until no-one are left. Note that the both victims (sorry, trainees) leave the ring simultaneously, so it was possible for one official to count a P Erson already selected by the other official.


Write a program that would successively read in (on that order) the three numbers (n, K and M; k, m > 0, 0 < N < 2 0) and determine the order in which the applicants is sent off for retraining. Each set of three numbers would be on a separate line and the end of data would be signalled by three zeroes (0 0 0).


For each triplet, output a single line of numbers specifying the order in which people is chosen. Each number should is in a field of 3 characters. For pairs of numbers lists the person chosen by the counter-clockwise official first. Separate successive pairs (or singletons) by commas (but there should not be a trailing comma).

Sample input

10 4 3
0 0 0
Sample output

4 8, 9 5, 3 1, 2 6, 10, 7
Similarly, Joseph questions a group of people sit in a circle, there are two people named, the first person clockwise to find the K, the second person counter-clockwise to find the first m. The output is the person each time it is ordered. If two people point to the same then output one.

#include <bits/stdc++.h>
using namespace std;
int n,k,m;
int a[101];
int go (int p,int d,int t)
    while (t--)
            p= (p+d+n-1)%n+1;
        while (a[p]==0);
    return p;
int main ()
    Ios::sync_with_stdio (false);
    while (Cin>>n>>k>>m,n+k+m)
        (int i=1;i<=n;i++)
        int left=n;
        int p1=n,p2=1;
        while (left)
            p1=go (p1,1,k);
            P2=go (p2,-1,m);
            COUT<<SETW (3) <<p1;
            if (P2!=P1)
                COUT<<SETW (3) <<p2;
            if (left)
                cout<< ",";
    return 0;

Recently no less write code, especially when the problem, found that their code is always modified after writing, and always have a variety of bugs, encountered a complex point of the simulation, but also the circle. So still write from the foundation, the Purple Book fourth chapter above the title to do.

The obvious problem of Joseph, can be solved by the list, but more trouble, with STL writing is also more laborious. Read the code on the book is very ingenious, in fact, the author of the top-down code to build the method of their own usually used, may be logic is not clear enough, or incomplete, more practice some.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.