1, 2 ,..., n of the n people sit at the Round Table clockwise. Each person holds a password (positive integer), starts reporting the number of s people, reports the number to the m people, and reports the number from the next person, until all members are listed
Below is the C language source program compiled with a linked list.
# Include <stdio. h>
# Include <malloc. h>
Typedef struct Joseph
{
Int num;
Struct Joseph * next;
} Joseph, * linklist;
Main ()
{
Int I, j, n, s, password;
Struct Joseph * head, * p1, * p2;
Head = (struct Joseph *) malloc (sizeof (Joseph ));
P1 = (struct Joseph *) malloc (sizeof (Joseph ));
P2 = (struct Joseph *) malloc (sizeof (Joseph ));
Printf ("input the value of n :");
Scanf ("% d", & n );
Printf ("input the number of the beginer :");
Scanf ("% d", & s );
If (s> n)
{
Printf ("please input a number smaller than % d:", n );
Scanf ("% d", & s );
}
Printf ("input the value of the password :");
Scanf ("% d", & password );
Printf ("the output is: n ");
P1-> num = 1;
P2-> num = 2;
Head-> next = p1;
P1-> next = p2;
P1 = p1-> next;
For (I = 3; I <= n; I ++)
{
P2 = (struct Joseph *) malloc (sizeof (Joseph ));
P1-> next = p2;
P1 = p1-> next;
P1-> num = I;
If (p1-> num = n)
P1-> next = head-> next;
}
P1 = head-> next;
For (I = 1; I <s; I ++)
P1 = p1-> next;
For (I = 1; I <= n; I ++)
{
If (password! = 1)
{
For (j = 1; j <password-1; j ++)
P1 = p1-> next;
Printf ("% 4d", p1-> next-> num );
P1-> next = p1-> next;
P1 = p1-> next;
}
If (password = 1)
{
Printf ("% 4d", p1-> num );
P1 = p1-> next;
}
}
Printf ("nn ");