猴子选大王的题,就是不知道哪里错了。。。
练习2: 1)题目描述:n只猴子要选大王,选举方法如下:所有猴子按 1,2 ……… n 编号并按照顺序围成一圈,从第 k 个猴子起,由1开始报数,报到m时,该猴子就跳出圈外,下一只猴子再次由1开始报数,如此循环,直到圈内剩下一只猴子时,这只猴子就是大王。2)输入数据:猴子总数n,起始报数的猴子编号k,出局数字m
3)输出数据:猴子的出队序列和猴子大王的编号
#include<stdio.h>
#include<malloc.h>
#define MONKEY_NUMBER 50
typedef int datatype;
typedef struct node
{
datatype number;
struct node *next;
}Linklist;
Linklist *head,*s,*t,*r;
Linklist *p;
int main()
{
void Sign_number(Linklist *,Linklist*,Linklist*);
void Circulate_main(Linklist *,Linklist*,Linklist*);
t = (Linklist*)malloc(sizeof(Linklist));
head = t;
s = (Linklist*)malloc(sizeof(Linklist));
Sign_number(head,s,t);
Circulate_main(head,s,t);
return 0;
}
void Sign_number(Linklist *head,Linklist *s,Linklist *t)
{
int i,j,k;
for(i = 0;i < MONKEY_NUMBER;i++)
{
s = (Linklist*)malloc(sizeof(Linklist));
s->number = i + 1;
t->next = s;
t = s;
}
s->next = head->next;
s = head->next;
for(j = 0;j < 50;j++)
{
printf("%2d ",s->number);
s = s->next;
}
}
void Circulate_main(Linklist *head,Linklist *s,Linklist *t)
{
int i,j,k;
s = head->next;
t = head;
while(s->number != s->next->number)
{
i = 0;
while(i < 4-1)
{
p = s->next;
t = t->next;
s = p;
}
p = s->next;
s->next = p->next;
printf("%d",s->number);
s = s->next;
free(p);
}
}