C Programming Problem: josephus problem Solution

You can do it with a circular linked list!

# include & ltstdio.h & gt

Structural node {

Int data;

Structure node * next

} node, * list, *p, * r;;

void JOSEPHU(int n,int k,int m)

{

int i,j;

list = NULL

for(I = 1; I < = n; i++)

{

P= (structural node *)malloc(sizeof (node));

p->; Data = I;;

if(list==NULL)

list = p;

other

r-& gt; next = p;

r = p;

}

p->; Next = list/* Create a circular linked list */

P = list;

for(I = 1; I< = n+1; i++)

{

printf("%d ",p-& gt; Data);

p = p-& gt; Next;

}

printf(" \ n "); /* Print the linked list and check whether the circular linked list is entered correctly */

P = list;

I = 1;

And (P & I<k)

{ r = p;

p = p-& gt; Next;

++ I;

}

for(I = 1; I & ltn;; i++)

{

for(j = 1; j & ltm; j++)

{ r = p;

p = p-& gt; Next;

}

printf(" out = % d \ n ",p-& gt; Data);

r-& gt; next = p-& gt; Next;

}

}

void main()

{

int x,y,z;

Printf ("input length n \ n"); /* n, k and m represent the total number, the first person to count off, and the interval number */

scanf("%d ",& ampx);

Printf ("input start k \ n");

scanf("%d ",& ampy);

Printf ("enter m \ n");

scanf("%d ",& ampz);

Joseph (x, y, z);

}