Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
12/12講座問題2
#include <stdio.h>
#include <algorithm>
using namespace std;
class Student {
public:
char name;
int math, eng;
bool operator < (const Student &s ) const {
if(math == s.math)
return eng > s.eng;
return math > s.math;
}
};
int main() {
Student stu[5];
stu[0].name = 'a'; stu[0].math = 2; stu[0].eng = 8;
stu[1].name = 'b'; stu[1].math = 9; stu[1].eng = 3;
stu[2].name = 'c'; stu[2].math = 8; stu[2].eng = 3;
stu[3].name = 'd'; stu[3].math = 2; stu[3].eng = 2;
stu[4].name = 'e'; stu[4].math = 5; stu[4].eng = 3;
sort( stu, stu + 5);
for(int i=0; i<5; i++) {
printf("%c %d %d\n", stu[i].name, stu[i].math, stu[i].eng);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment