帮忙找一下原因--猴子选大王
为什么输入6,5会出现2?应该是一~~~#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct M
{
int num;
struct M *next;
};
int main(void)
{
int i, j, n, t;
struct M *p, *q, *head;
p = (struct M*)malloc(sizeof(struct M));
printf("The number of monkeys: ");
scanf("%d", &n);
printf("The amount of count: ");
scanf("%d", &t);
for(i = 1;i <= n;i++)
{
q = (struct M*)malloc(sizeof(struct M));
q->num = i;
if(i == 1) head = q;
p->next = q;
p = q;
}
p->next = head;
p = head;
for(i = 0;i <= n-2;i++)
{
for(j = 0;j <= t-1;j++)
{
q = p;
p = p->next;
}
q->next = p->next;
free(p);
p = q;
}
printf("The king: %d\n", p->num);
return 0;
}
谢谢