c语言来写多关键字排序排序。
发布网友
发布时间:2023-09-02 20:22
我来回答
共1个回答
热心网友
时间:2024-12-02 03:37
#include<stdio.h>
typedef struct
{
float total,shuxue,yuwen,lizong;
}Score_Kind;
int cmp(const Score_Kind * a,const Score_Kind *b)
{
if(a->total!=b->total)
return a->total-b->total;
if(a->shuxue!=b->shuxue)
return a->shuxue-b->shuxue;
if(a->yuwen!=b->yuwen)
return a->yuwen-b->yuwen;
return a->lizong-b->lizong;
}
int main ()
{
Score_Kind scores[10]={1,2,3,4
,5,6,7,8
,3,2,1,6
,4,3,2,5
,1,4,3,6
,4,2,6,7
,3,5,3,6
,5,3,2,6
,5,8,3,1};
Score_Kind tmp;
int i,k,j;
int n=9;
for (i=0;i<n;i++)
{
k=i;
for (j=i+1;j<n;j++)
{
if (cmp(scores+k,scores+j)<0)
{
k=j;
}
}
tmp=scores[k];
scores[k]=scores[i];
scores[i]=tmp;
}
for (i=0;i<n;i++)
{
printf("%f\t%f\t%f\t%f\n",scores[i].total,scores[i].shuxue,scores[i].yuwen,scores[i].lizong);
}
}