利用sort函数成绩排序
其中库函数strcmp(a,b)是对字典中的字母进行排序,有一个返回值,若为0则表示相同,若为负数则表示a的序列在b之后,正数则是a的序列在b之前。
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
/*成绩排序*/
struct E{
char name[101];
int age;
int score;
}buf[10000];
bool cmp(E a,E b){
if(a.score!=b.score)
return a.score<b.score;//若分数 不相同则分数小的在前
int tem=strcmp(a.name,b.name);
if(tem!=0) return tem<0;//分数相同,则名字字典序小的在前
else{
return a.age<b.age;//年龄小的在前
}
}
int main(){
int n;
cin>>n;
while(n>0){
for(int i=0;i<n;i++){
cin>>buf[i].name>>buf[i].age>>buf[i].score;
}
sort(buf,buf+n,cmp);
for(int i=0;i<n;i++){
cout<<buf[i].name<<" ";
cout<<buf[i].age<<" ";
cout<<buf[i].score<<" ";
}
}
return 0;
}