刚才发现了两个小问题,现在成绩已经能正确排序了,但是学号还是有问题:
#include <stdio.h>
#include <stdlib.h>
void sift(int startPos,int endPos,int score[],int number[])
{
int k=startPos*2+1,temp1,temp2;
temp1=score[startPos];
temp2=number[startPos];
while(k<endPos)
{
if(k+1<endPos&&score[k]<score[k+1])k++;
else if(k+1<endPos&&score[k]==score[k+1]&&number[k]>number[k+1])k++;
if(temp1<score[k])
{
score[startPos]=score[k];
startPos=k;
k=startPos*2+1;
}
else if(score[k]==temp1&&number[k]>number[k+1])
{
score[startPos]=score[k];
startPos=k;
k=startPos*2+1;
}
else break;
}
score[startPos]=temp1;
number[startPos]=temp2;
}
void heap_sort(int number[],int score[],int n)
{
int i,temp;
for(i=n/2;i>=0;i--)
sift(i,n,score,number);
for(i=n-1;i>=0;i--)
{
temp=number[i];number[i]=number[0];number[0]=temp;
temp=score[i];score[i]=score[0];score[0]=temp;
sift(0,i,score,number);
}
}
int main()
{
int number[10]={0},score[10]={0},n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d %d",&number[i],&score[i]);
heap_sort(number,score,n);
printf("\n");
for(i=0;i<n;i++)
printf("%d %d\n",number[i],score[i]);
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void sift(int startPos,int endPos,int score[],int number[])
{
int k=startPos*2+1,temp1,temp2;
temp1=score[startPos];
temp2=number[startPos];
while(k<endPos)
{
if(k+1<endPos&&score[k]<score[k+1])k++;
else if(k+1<endPos&&score[k]==score[k+1]&&number[k]>number[k+1])k++;
if(temp1<score[k])
{
score[startPos]=score[k];
startPos=k;
k=startPos*2+1;
}
else if(score[k]==temp1&&number[k]>number[k+1])
{
score[startPos]=score[k];
startPos=k;
k=startPos*2+1;
}
else break;
}
score[startPos]=temp1;
number[startPos]=temp2;
}
void heap_sort(int number[],int score[],int n)
{
int i,temp;
for(i=n/2;i>=0;i--)
sift(i,n,score,number);
for(i=n-1;i>=0;i--)
{
temp=number[i];number[i]=number[0];number[0]=temp;
temp=score[i];score[i]=score[0];score[0]=temp;
sift(0,i,score,number);
}
}
int main()
{
int number[10]={0},score[10]={0},n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d %d",&number[i],&score[i]);
heap_sort(number,score,n);
printf("\n");
for(i=0;i<n;i++)
printf("%d %d\n",number[i],score[i]);
system("pause");
return 0;
}
欢迎来到我的博客:http://blog..cn/noisunyuhong