- 題目:學生有兩門課語文和數學,實現一個班級名次排名,要求總分高的排在前面,當總分相同時,數學成績高的在前面,若兩門課程均相等時,按姓名拼音排序。
- 輸入(第一行輸入 幾個學生,第二行輸入學生姓名,語文成績,數學成績)
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;
}