CSP歷年複賽題-P1093 [NOIP2007 普及組] 獎學金

江城伍月發表於2024-05-25

原題連結:https://www.luogu.com.cn/problem/P1093

題意解讀:本題考察排序,根據題意,先按總分從大到小排,再按語文從大到小排,以上都相同則按學號從小到大排。

100分程式碼:

#include <bits/stdc++.h>
using namespace std;

const int N = 305;

struct student
{
    int id;
    int yuwen;
    int shuxue;
    int yingyu;
    int total;
} s[N];

bool cmp(student s1, student s2)
{
    if(s1.total != s2.total) return s1.total > s2.total;
    if(s1.yuwen != s2.yuwen) return s1.yuwen > s2.yuwen;
    return s1.id < s2.id;
}

int n;

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        s[i].id = i;
        cin >> s[i].yuwen >> s[i].shuxue >> s[i].yingyu;
        s[i].total = s[i].yuwen + s[i].shuxue + s[i].yingyu;
    }

    sort(s + 1, s + n + 1, cmp);

    for(int i = 1; i <= 5; i++) cout << s[i].id << " " << s[i].total << endl;

    return 0;
}

相關文章