编译通过了,但提示段错误,不知道错误原因。
题目是让我们寻找大富翁,先输入N,M. N代表总人数,M代表要找出大富翁数。之后再输入N个大富翁资产值。要我们以非递增方式输出前M个大富翁资产值。
我的代码如下:结果提示编译通过,但是段错误。
程序代码:#include<stdio.h>
#define MAXM 10
typedef int ElementType;
void InsertionSort(ElementType A[],int N)
{
int i,j;
ElementType temp;
for(i=1;i<N;i++){
temp=A[i];
for(j=i;(j>0)&&(temp>A[j-1]);j--)
A[j]=A[j-1];
A[j]=temp;
}
}
void Adjust(ElementType A[],int i,int N)
{
int Child;
ElementType temp;
for(temp=A[i];(2*i+1)<N;i=Child){
Child=(2*i+1);
if((Child!=N-1) && A[Child+1]<A[Child])
Child++;
if(temp>A[Child])
A[i]=A[Child];
else break;
}
A[i]=temp;
}
int main()
{
int N,M,i;
ElementType A[MAXM],temp;
scanf("%d %d",&N,&M);
if(N>MAXM){
for(i=0;i<M;i++)
scanf("%d",&A[i]);
for(i=(M-1)>>1;i>=0;i--)
Adjust(A,i,M);
for(i=M;i<N;i++){
scanf("%d",&temp);
if(temp>A[0]){
A[0]=temp;
Adjust(A,i,M);
}
}
for(i=M-1;i>0;i--){
temp=A[0];A[0]=A[i];A[i]=temp;
Adjust(A,0,i);
}
}
else{
for(i=0;i<N;i++)
scanf("%d",&A[i]);
InsertionSort(A,N);
}
if(N<M) M=N;
printf("%d",A[0]);
for(i=1;i<M;i++)
printf(" %d",A[i]);
return 0;
}









