Joseph ring
N individuals are numbered sequentially in a circle, starting from the 1st, press 1, 2, 3. Report the number in order, those who report p withdraw from the circle, the rest start from 1, 2, 3, those who report p then quit the circle, and so on. Please output the original serial number of each quitter in exit order.
-sharpinclude<stdio.h>
-sharpinclude<stdlib.h>
struct node{
int data1;
int data2;
struct node *next;
};
int main(void){
struct node *head, *p, *q, *t;
int i, N, P, j;
scanf("%d %d", &N, &P);
head = NULL;
for (i = 1; i <= N; iPP){
p = (struct node*)malloc(sizeof(struct node));
p->data1 = i;
p->next = NULL;
if (head == NULL)
head = p;
else
q->next = p;
q = p;
}
q->next = head;
t = head;
while (t != NULL)
{
if (t->next->data1 == P){
printf("%d ", t->next->data1);
t->next = t->next->next;
j = 0;
jPP;
for (i = 1; i <= N - j; iPP){
t->next->data2 = i;
t = t->next;
}
}
t = t->next;
if(t->next==t){
printf("%d",t->data1);
break;
}
}
getchar(); getchar();
return 0;
}