编写一个程序,基于结构体存储N
个学生信息,包含学号,姓名和学科成绩,并使用模板函数sort
完成对学生信息的排序:成绩高的排序靠前,若成绩相同,则学号小的排序靠前。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
struct Student{
int numberID;
char name[20];
int score;
Student(){}
Student(int id_, char *name_, int score_){
numberID = id_;
strcpy(name, name_);
score = score_;
}
void in(){
scanf("%d %s %d", &numberID, name, &score);
}
void out() {
printf("%d %s %d\n", numberID, name, score);
}
bool operator < (const Student &S)const {
if(this->score==S.score){
return this->numberID<S.numberID;
}
else{
return this->score>S.score;
}
}
};
bool max_cmp(Student S1, Student S2){
if(S1.score==S2.score){
return S1.numberID<S2.numberID;
}
else{
return S1.score>S2.score;
}
}
int main(int argc, const char * argv[])
{
int n;
cin>>n;
Student *S=new Student[n];
for(int i=0;i<n;i++){
S[i].in();
}
sort(S,S+n,max_cmp);
for(int i=0;i<n;i++){
S[i].out();
}
return 0;
}