#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, c;
struct student{
char num[10];
char name[10];
int grade;
}stu[maxn];
void heapAjust(int head, int tail, bool (*cmp)(student, student)){
student value = stu[head];
int l = 2*head, r = 2*head+1;
int id;//较大的孩子
while(l<=tail){
if(r<=tail && (cmp(stu[l], stu[r]))) id = r;
else id = l;
if((cmp(value, stu[id]))){//堆顶小
stu[head] = stu[id];//孩子上移
head = id; l = id*2; r = id*2+1;
}
else break;
}//跳出循环时 head满足最大堆
stu[head] = value;
}
void heapSort(bool (*cmp)(student, student)){
for(int i = n/2; i > 0; i--)
heapAjust(i, n, cmp);
for(int i = 1; i <= n-1; i++){
student tmp = stu[1];
stu[1] = stu[n-i+1];
stu[n-i+1] = tmp;
heapAjust(1, n-i, cmp);
}
}
bool cmp1(student a, student b){
if(strcmp(a.num, b.num)<0) return 1;
return 0;
}
bool cmp2(student a, student b){
if(strcmp(a.name, b.name) == 0) return cmp1(a, b);
if(strcmp(a.name, b.name)<0) return 1;
return 0;
}
bool cmp3(student a, student b){
if(a.grade == b.grade) return cmp1(a, b);
return a.grade < b.grade;
}
int main(){
scanf("%d%d",&n,&c);
for(int i = 1; i <= n; i++)
scanf("%s%s%d",&stu[i].num,&stu[i].name,&stu[i].grade);
if(c==1) heapSort(cmp1);//sort(stu+1, stu+n+1, cmp1);
else if(c==2) heapSort(cmp2);//sort(stu+1, stu+n+1, cmp2);
else if(c==3) heapSort(cmp3);//sort(stu+1, stu+n+1, cmp3);
for(int i = 1; i <= n; i++)
printf("%s %s %d\n",stu[i].num, stu[i].name, stu[i].grade);
return 0;
}
仅供参考 切勿抄袭
hang hang hang !!!