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.
Input
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).
Output
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
English:
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--)
{
do
{
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)
{for
(int i=1;i<=n;i++)
a[i]=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;
left--;
if (P2!=P1)
{
COUT<<SETW (3) <<p2;
left--;
}
a[p1]=a[p2]=0;
if (left)
cout<< ",";
}
cout<<endl;
}
return 0;
}
Answer:
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.