排序(多种情况)
成绩排序
https://ac.nowcoder.com/acm/contest/71298/G
输入n个学生的姓名和三科(编程,数学,英语)的成绩,按照三科的总分从高到低排序输出姓名和总分,如果总分相同,姓名按照字典序排序输出。
输入描述: 共n + 1行,
第一行,一个整数n(1 < n <100),
接下来的n行,每行输入一个字符串,长度小于100,表示姓名,接下来空一个格,输入三个整数,用空格隔开,表示三科成绩。
输出描述: 共n行,按照三科的总分从高到低排序输出姓名和总分,每行输出一个姓名和总分,用空格分隔。代码详见: #include #include #include #include
using namespace std;
struct Student {
string name;
int programming;
int math;
int english;
int total_score;
};
bool compare(const Student &a, const Student &b) {
if (a.total_score == b.total_score) {
return a.name < b.name;
}
return a.total_score > b.total_score;
}
int main() {
int n;
cin >> n;
vector<Student> students(n);
for (int i = 0; i < n; i++) {
cin >> students[i].name;
cin >> students[i].programming >> students[i].math >> students[i].english;
students[i].total_score = students[i].programming + students[i].math + students[i].english;
}
sort(students.begin(), students.end(), compare);
for (int i = 0; i < n; i++) {
cout << students[i].name << " " << students[i].total_score << endl;
}
return 0;
}