During the interview, you are often asked to report games. Implement it.
Problem description:
There are n people sitting around and starting from 1 to n in clockwise direction, starting from the second person 1 to M reports, when the number is up to m people, this person goes out of the circle, then start the 1-M report from his next person, so proceed until all people are out of the circle. Now we need to print out the circle order.
Input: N game total number S start number M Value
Output: P points to an array with N length, and the outbound order is stored in the array pointed to by P.
Example
N = 7 S = 2 m = 3 outbound order: 4 7 3 1 6 2 5
N = 3 S = 1 m = 2 outbound order: 2 1 3
Use a circular linked list to implement the following:
# include
using namespace STD; typedef struct tagnode {int data; struct tagnode * Next;} node; void Joseph (INT count, int start, int steps) {node * P, * q, * head; head = (node *) New node; P = head; for (INT I = 1; I <= count-1; I ++) {P-> DATA = I; P-> next = (node *) New node; P = p-> next;} p-> DATA = count; // The final special handling of the last player to form a circular linked list p-> next = head; P = head; while (p-> next! = P) {While (p-> data! = Start) P = p-> next; If (Steps = 1) {for (INT I = 1; I <= count; I ++) {cout
data <"\ t"; P = p-> next;} cout
next; // P points to the first member to be listed} q = p-> next; cout
data <"\ t"; // Q is the member to be listed p-> next = Q-> next; // reconnects the linked list delete Q; start = p-> next-> data; // P indicates that the next player is the first player in the next round.} cout
data; // output the last column member Delete P; cout
count> Start> steps; Joseph (count, start, steps); Return 0 ;}