CCF NOI1042. 发奖学金 (C++)

80 篇文章 6 订阅

1042. 发奖学金

题目描述

某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金。期末,每个学生都有3门课的成绩:语文、数学、英语。先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规定学号小的同学排在前面,这样,每个学生的排序是唯一确定的。

任务:先根据输入的3门课的成绩计算总分,然后按上述规则排序,最后按排名顺序输出前五名名学生的学号和总分。注意,在前5名同学中,每个人的奖学金都不相同,因此,你必须严格按上述规则排序。例如,在某个正确答案中,如果前两行的输出数据(每行输出两个数:学号、总分) 是:
7 279
5 279
这两行数据的含义是:总分最高的两个同学的学号依次是7号、5号。这两名同学的总分都是 279 (总分等于输入的语文、数学、英语三科成绩之和) ,但学号为7的学生语文成绩更高一些。如果你的前两名的输出数据是:
5 279
7 279
则按输出错误处理,不能得分。

输入

包含n+1行: 第1行为一个正整数n,表示该校参加评选的学生人数。
第2到n+1行,每行有3个用空格隔开的数字,每个数字都在0到100之间。第j行的3个数字依次表示学号为 j-1 的学生的语文、数学、英语的成绩。每个学生的学号按照输入顺序编号为1~n (恰好是输入数据的行号减1)。所给的数据都是正确的,不必检验。

输出

共有5行,每行是两个用空格隔开的正整数,依次表示前5名学生的学号和总分。

样例输入

6
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98

样例输出

6 265
4 264
3 258
2 244
1 237

数据范围限制

50%的数据满足:各学生的总成绩各不相同;
100%的数据满足: 6<=n<=300。

C++代码,但使用了C的函数 qsort()

#include <iostream>
#include <cassert>
#include <algorithm>

using namespace std;

const int N = 300; // maximum n

// define a new struct type Student
struct _Student
{
    int ID;
    int Chinese; 
    int Math;   
    int English;
    int total;
}; 
typedef struct _Student Student; 

// define my sorting criteria function
int cmp(const void *s1, const void *s2)
{
    if (((Student *)s1)->total == ((Student *)s2)->total)
    {
        if (((Student *)s1)->Chinese == ((Student *)s2)->Chinese)
        {
            // sort ascending by Student ID
            return ((Student *)s1)->ID - ((Student *)s2)->ID; 
        }
        else
        {
            // sort descending by Chinese score
            return ((Student *)s2)->Chinese - ((Student *)s1)->Chinese; 
        }
    }
    else
    {
        // sort descending by total score
        return ((Student *)s2)->total - ((Student *)s1)->total; 
    }
}

int main()
{
    int n;

    cin >> n;

    assert(6 <= n && n <= N);

    Student s;
    Student AllStudents[N];

    for(int i=1; i<=n; i++)
    {
        cin >> s.Chinese >> s.Math >> s.English;

        assert(0 <= s.Chinese && s.Chinese <= 150);
        assert(0 <= s.Math    && s.Math    <= 150);
        assert(0 <= s.English && s.English <= 150);

        s.ID    = i;
        s.total = s.Chinese + s.Math + s.English;

        AllStudents[i-1] = s;
    }
    
    qsort(AllStudents, n, sizeof(Student), cmp);

    for(int i=1; i<=5; i++)
    {
        cout << AllStudents[i-1].ID << " " << AllStudents[i-1].total << endl;
    }

    return 0;
}

另外一种求解,使用了C++函数 sort()

#include <iostream>
#include <cassert>
#include <algorithm>

using namespace std;

const int N = 300; // maximum n

// define a new struct type Student
struct _Student
{
    int ID;
    int Chinese; 
    int Math;   
    int English;
    int total;
}; 
typedef struct _Student Student;

// define my sorting criteria function
bool cmp(Student& s1, Student& s2)
{
    if (s1.total == s2.total)
    {
        if (s1.Chinese == s2.Chinese)
        {
            // sort ascending by Student ID
            return (s1.ID < s2.ID); 
        }
        else
        {
            // sort descending by Chinese score
            return (s1.Chinese > s2.Chinese); 
        }
    }
    else
    {
        // sort descending by total score
        return (s1.total > s2.total); 
    }
}

int main()
{
    int n;

    cin >> n;

    assert(6 <= n && n <= N);

    Student stu;
    Student AllStudents[N];

    for(int i=1; i<=n; i++)
    {
        cin >> stu.Chinese >> stu.Math >> stu.English;

        assert(0 <= stu.Chinese && stu.Chinese <= 150);
        assert(0 <= stu.Math    && stu.Math    <= 150);
        assert(0 <= stu.English && stu.English <= 150);

        stu.ID    = i;
        stu.total = stu.Chinese + stu.Math + stu.English;

        AllStudents[i-1] = stu;
    }

    sort(AllStudents, AllStudents+n, cmp);

    for(int i=1; i<=5; i++)
    {
        cout << AllStudents[i-1].ID << " " << AllStudents[i-1].total << endl;
    }

    return 0;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值