- 题目:学生有两门课语文和数学,实现一个班级名次排名,要求总分高的排在前面,当总分相同时,数学成绩高的在前面,若两门课程均相等时,按姓名拼音排序。
- 输入(第一行输入 几个学生,第二行输入学生姓名,语文成绩,数学成绩)
3
abc 88 89
aaa 88 89
bcd 87 90
bcd 87 90
aaa 88 89
abc 88 89
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef struct{
char name[100];
int chinese;
int math;
}student;
student stu[1001];
int compare(const void* a, const void* b){
student* pa = (student*) a;
student* pb = (student*) b;
int num1 = pa->chinese + pa->math;
int num2 = pb->chinese + pb->math;
return num2 - num1;
}
int main(){
int n;
scanf("%d", &n);
student temp;
for(int i = 0; i < n; i++){
scanf("%s %d %d", &stu[i].name, &stu[i].chinese, &stu[i].math);
}
qsort(stu, n, sizeof(student), compare);
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if((stu[i].chinese + stu[i].math) == (stu[j].chinese + stu[j].math)){
if(stu[i].math < stu[j].math){
temp = stu[j];
stu[j] = stu[i];
stu[i] = temp;
}else if((stu[i].chinese == stu[j].chinese) && (stu[i].math == stu[j].math)){
if(strcmp(stu[i].name, stu[j].name) > 0){
temp = stu[j];
stu[j] = stu[i];
stu[i] = temp;
}
}
}
}
}
for(int i = 0; i < n; i++){
printf("%s\t%d\t%d\n", stu[i].name, stu[i].chinese, stu[i].math);
}
return 0;
}