Joseph PHY: Assume that N people are sitting around the Round Table, reporting data from S people, and reporting data to M,
Then report the number from the next person, and the number of M people will be listed as follows:
Column. Requires that the information of each person be output in sequence of Columns
# Include <stdio. h>
# Include <stdlib. h>
Typedef int datatype;
Typedef struct cnode
{
Datatype data;
Struct cnode * next;
} Clistnode;
Typedef clistnode * clinklist;
Clinklist head;
Void Joseph (int n, int m, int s)
{
Int I, j, flag;
Clinklist creatclinklist (int);/* function declaration: Creates a circular linked list using the element forward insertion method. The length is limited to N */
Void print (clinklist head, int );
Clistnode * p, * q, * r, * t;
Head = creatclinklist (n );
Print (head, n);/* print the elements of the circular linked list in order */
Q = head;
If (s> n) printf ("error! N ");
Flag = s % n;/* move the pointer q to the second node and give it to the pointer p to minimize the number of pointer moves! */
For (I = 1; I <flag; I ++)
Q = q-> next;
P = q-> next;
Printf ("% d node in the order of departure is; n", n );
For (I = 1; I <= n; I ++)/* records I as the number of deleted nodes */
{
J = 1;
Flag = m % (n-I + 1);/* move the pointer p to the M node to minimize the number of pointer moves! Try to make the number of moves per time
No more than n times! Time complexity O (n), improving the execution efficiency of the program! */
If (! Flag)/* do not know how to move this step! Special Handling of some nodes! */
{
J = 1;
While (j <m)
{
R = p;
P = p-> next;
If (p = head)
Continue;
J ++;
}
}