/// Joseph Ring
// Time: 05/07/04
// Program: Zhang jianbo
// Input n = 7 // 3, 1, 7, 2, 4, 4 // output 6 7 4 1 5 3 2
# Include <iostream. h>
# Include "key. H"
Typedef struct person
{Int PWD; // Password
Int num; // number of people
Struct person * Next; // pointer to the struct
} Person;
Void output (int * a, int N); // output result
Int creatpersonlist (person * head); // create a linked list
Int fx_personlist (person * head, int N, int * A); // analog report count
Int _ f4_main () // function entry
{
Person * head; // defines the head of the linked list.
Cout <"Joseph Ring problem simulation! "<Endl;
Head = new person; // create a person and allocate memory
Int n = creatpersonlist (head); // create a linked list and return n people
Int * A = new int [20]; // Save the output result
Int x = fx_personlist (Head, n, a); // simulate the number of records and return the number of outgoing records count
Output (A, x); // output sequence
Initkey (); // keyboard interrupt
Return 0;
}
Int creatpersonlist (person * head ){
Int n, m, I;
Person * P, * q;
Cout <"/N, enter n = ";
Cin> N;
P = head; // P points to the header
For (I = 1; I <= N; I ++) {// create a linked list
For cout <"/N, enter the" <I <"reporter password M = ";
Cin> m;
P-> Pwd = m;
P-> num = I;
Q = new person;
If (I = N) P-> next = head; // when I = N, Point P1 to the header at the end of the loop
Else
{
P-> next = Q;
P = Q;
}
}
Return N;
}
Int fx_personlist (person * head, int N, int * ){
Person * P1, * P0, * P;
Int TM; // temporary variable, save the previous password value
Int m; // password m
Int COUNT = 1; // Number of outgoing queues
P0 = p1 = head; // make P0, P1 point to head Header
Cout <"/n enter the start value of M = ";
Cin> m;
While (count <= N-1)
{
For (INT I = 1; I <m; I ++)
{// Re-use m as the upper limit of the report count
P1 = P0;
P0 = P0-> next;
TM = P0-> pwd;
}
M = TM;
P = P0-> next;
A [count ++] = P0-> num; // Save the serial number of each departure in a [x]
P1-> next = P0-> next;
Delete P0;
P0 = P;
}
A [count] = P0-> num; // Save the serial number of each departure in a [x]
P1-> next = P0-> next;
Delete P0;
Return count;
}
Void output (int * a, int N)
{
Cout <"the correct output sequence is :";
For (INT I = 1; I <= N; I ++) cout <A [I] <"";
}