首页 > 试题广场 >

成绩排序

[编程题]成绩排序
  • 热度指数:228 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

高一年级(1)班的小美老师需要对学生的考试成绩进行处理。单科考试的卷面总分为150分,考生得分一定为整数。

首先按照总分由大到小排序,如果总分相等则按照语文成绩由大到小排序。(输入保证没有两名学生的成绩完全相等)。

输入n个学生的学号和成绩(n<=1000),请输出按要求排序后所有学生的学号。


输入描述:

单组输入,第1行为n。

接下来n行每一行包含三个整数,分别为学号(>=1的任意正整数,同一组输入保证学号不重复)、语文成绩和数学成绩。



输出描述:

输出按要求排序后所有学生的学号,两个学号之间用空格隔开。

示例1

输入

5
10001 95 90
10002 96 96
10003 90 95
10004 100 88
10005 98 94

输出

10005 10002 10004 10001 10003
直接按总分和语文成绩对成绩单进行二次排序,然后遍历输出学号就行
n = int(input())
report_card = []
for _ in range(n):
    report_card.append(list(map(int, input().strip().split())))
report_card = sorted(report_card, key=lambda x: (-x[1] - x[2], -x[1]))
for item in report_card:
    print(item[0], end=' ')


发表于 2021-03-08 17:58:10 回复(0)
// 总分由大到小排序,如果总分相等则按照语文成绩由大到小排序。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class Stu{
    public:
    int id;
    int chinese;
    int math;
    int sum;
    Stu(int id,int chinese,int math){
        this->id=id;
        this->chinese=chinese;
        this->math=math;
        this->sum=math+chinese;
    }
};
bool myCompare(Stu s1,Stu s2){
    if(s1.sum==s2.sum){
        return s1.chinese>s2.chinese;
    }
    return s1.sum>s2.sum;
}

void printId(vector<Stu> v){
    auto it=v.begin();
    for(;it!=v.end();it++){
        cout<<(*it).id<<" ";
    }
}

int main(){
    int num;
    cin>>num;
    vector<Stu> v;
    while (num)
    {
        int id;
        int chinese;
        int math;
        cin>>id>>chinese>>math;
        Stu s_tmp(id,chinese,math);
        v.push_back(s_tmp);
        num--;
    }
    
    sort(v.begin(),v.end(),myCompare);
    printId(v);
    // system("pause");
    return 0;
}

发表于 2023-01-06 11:04:13 回复(0)
pair很好用。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool ccmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
    if (a.second.first > b.second.first) { return true; }
    else if (a.second.first < b.second.first) { return false; }
    else {
        return a.second.second > b.second.second;
    }
}
int main() {
    int n,num1,num2;
    cin >> n;
    vector<pair<int, pair<int, int>>> res(n);
    for (int i = 0; i < n; i++) {
        cin >> res[i].first >> num1 >> num2;
        res[i].second.first = num1 + num2;
        res[i].second.second = num1;
    }
    sort(res.begin(), res.end(), ccmp);
    for (int i = 0; i < n; i++) {
        cout << res[i].first;
        if (i != n - 1) { cout << ' '; }
    }
    cout << endl;
    return 0;
}

发表于 2021-09-14 22:05:57 回复(0)