Problem Description
n只猴子要选大王,选举方法如下:所有猴子按 1,2 ……… n 编号并按照顺序围成一圈,从第 k 个猴子起,由1开始报数,报到m时,该猴子就跳出圈外,下一只猴子再次由1开始报数,如此循环,直到圈内剩下一只猴子时,这只猴子就是大王。
Input
猴子总数n,起始报数的猴子编号k,出局数字m。(2=<n<=20,k<=n)
Output
猴子的出队序列(不包括猴子大王)和猴子大王的编号。
Sample Input
6 5 9
3 3 11
Sample Output
1 5 6 4 2
3
1 2
3
HINT
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}linklist; int main()
{
int i, n, k, m, total;
linklist *head, *p, *s, *q;
while(scanf("%d%d%d",&n,&k,&m)!=EOF)
{
head = (linklist*) malloc(sizeof(linklist));
p = head;
p->data = ;
p->next = p;
for (i = ; i <= n; i++)
{
s = (linklist*) malloc(sizeof(linklist));
s->data = i;
s->next = p->next;
p->next = s;
p = p->next;
}
p = head;
for (i = ; i < k; i++)
{
p = p->next;
}
total = n;
q = head;
while (total != )
{
for (i = ; i < m; i++)
{
p = p->next;
}
if(total>)
printf("%d ", p->data);
else
printf("%d", p->data);
while (q->next != p)
{
q = q->next;
}
q->next = p->next;
s = p;
p = p->next;
free(s);
total--;
}
printf("\n%d\n", p->data);
free(p);
}
return ;
}