C语言约瑟夫问题

来源:百度知道 编辑:UC知道 时间:2024/09/20 11:40:59
我的程序在DEV C 和C-FREE 上都说我的申请动态内存错误。但在tc上能运行,为什么呢?
附程序
#include<stdio.h>
#include<time.h>
struct node
{ int number;
struct node *next;
};
main()
{ int n,m,i,k;
long start,end,total;
struct node *p,*q,*head;
head=(struct node*)malloc(sizeof(struct node));
head->number=-1;
head->next=head;
scanf("%d%d",&n,&m);
start=clock();
for(i=n;i>0;--i)
{
p=(struct node*)malloc(sizeof(struct node));
p->next=head->next;
p->number=i;
head->next=p;
}
while(p->next!=head)
p=p->next;
p->next=head->next;
if(m!=0)
{
for(i=0;i<n;i++)
{ for(k=1;k<m;k++)
p=p->next;
q=p->next;
p->next=q->next;
printf("%3d",q->number);
free(q);
}
}
else printf("%3d",n);
printf("\n");
end

VC6.0上编译运行都是正常的

不过我多包含了一个头文件:#include<stdlib.h>

否则编译不过,其他的没有任何修改

运行截图如下: