P1051 [NOIP2005 提高组] 谁拿了最多奖学金(stable_sort函数)

题解:一开始用了sort对奖学金进行排序,结果发现wa了一个点,后来下载数据一看。哦豁,原来我把奖学金相同的而且排名顺序靠后的人名输出了,找了一圈发现是sort的问题,sort是个不稳定的排序,稳定的sort排序写作stable_sort,换上去就好了。

#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
struct node {
    char name[1000];
    int average;
    int class_grade;
    bool student_leader;
    bool student_west;
    int paper;
    int scholarship;
} p[105];
ll n, sum;
inline bool cmp(node a, node b) {
    return a.scholarship > b.scholarship;
}
int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> p[i].name;
        cin >> p[i].average;
        cin >> p[i].class_grade;
        string str;
        cin >> str;
        if (str[0] == 'Y') {
            p[i].student_leader = true;
        } else {
            p[i].student_leader = false;
        }
        cin >> str;
        if (str[0] == 'Y') {
            p[i].student_west = true;
        } else {
            p[i].student_west = false;
        }
        cin >> p[i].paper;

        if (p[i].average > 80 && p[i].paper >= 1) {
            p[i].scholarship += 8000;
        }
        if (p[i].average > 85 && p[i].class_grade > 80) {
            p[i].scholarship += 4000;
        }
        if (p[i].average > 90) {
            p[i].scholarship += 2000;
        }
        if (p[i].average > 85 && p[i].student_west) {
            p[i].scholarship += 1000;
        }
        if (p[i].class_grade > 80 && p[i].student_leader) {
            p[i].scholarship += 850;
        }
        sum += p[i].scholarship;
    }
    stable_sort(p, p + n, cmp);
    cout << p[0].name << endl;
    cout << p[0].scholarship << endl;
    cout << sum << endl;
    return 0;
}

关于sort的详解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值