请大家看看这段代码怎么改进(我想养成好的编程习惯)。谢谢。

问题描述:

题目见图片。
下面是代码(这道题当然还有其他解法,我这里是刻意用链表做的)
{图片说明
#include
#include
typedef struct monkey{
int num;
struct monkey * next;
} M;
void game(int, int, int);
M create_list(M *,int );
int main(void)
{
int n;
scanf("%d", &n);
int i;
for (i = 1; i <= n; i++)
{
int n, m, p;
scanf("%d %d %d",&n, &m, &p);
game(n, m, p);
}
return 0;
}
void game(int n, int m, int p)
int j = 1;
M *headp = (M *)malloc(sizeof(M)),*tail;
tail=create_list(&headp,n);//创建大小为n的环状链表,返回尾节点
M *now = headp,*before=tail;
while (now->num != p)//定位到值为p的节点
{
before = now;
now = now->next;
}
while (n - 1)//进行n-1次游戏
{
int cnt = m - 1;
while (cnt)//定位到下一个被淘汰的猴子
{
before = now;
now = now->next;
--cnt;
}
M *temp = now;
before->next = now->next;
now = now->next;
free(temp);//删除被淘汰的猴子,定位到下一个
--n;
}
printf("%3d\n", now->num);//最后还剩一个猴子
}
M *create_list(M **head,int n)
{
M *temp = *head;
int i=1;
while (i !=n)//n个节点,依次赋值1,2....n
{
temp->num = i++;
temp->next = (M *)malloc(sizeof(M));
temp = temp->next;
}
temp->num = n;
temp->next = *head;
return temp;
}

你想看什么?编写规范还是执行效率?

这题不用答了,本来程序是有错的,后来自己发现了,就把题目从找错改成现在这样子,就这样吧。