OpenJudge-1.10.04:奖学金

一、题目链接

http://noi.openjudge.cn/ch0110/04/

二、解题思路(Java)

三、解题思路(C++)

四、Java程序

import java.util.Arrays;
import java.util.Scanner;

class Student implements Comparable<Student> { // 学生信息类,实现Comparable接口
    int id; // 标记学号
    int chs; // 标记语文成绩
    int math; // 标记数学成绩
    int en; // 标记英语成绩
    int total; // 标记三门课程的总分

    // 构造方法,用于初始化一个学生对象
    public Student(int id, int chs, int math, int en) {
        this.id = id;
        this.chs = chs;
        this.math = math;
        this.en = en;
        this.total = chs + math + en;
    }

    /* 重写Comparable接口的compareTo方法,实现主总分降序、次语文成绩降序、再学号升序的规则 */
    @Override
    public int compareTo(Student o) {
        if (o.total > this.total) {
            return 1;
        }
        else if (o.total < this.total) {
            return -1;
        }
        else {
            if (o.chs > this.chs) {
                return 1;
            }
            else if (o.chs < this.chs) {
                return -1;
            }
            else {
                if (this.id > o.id) {
                    return 1;
                }
                else if (this.id < o.id) {
                    return -1;
                }
                else {
                    return 0;
                }
            }
        }
    }

    /* 重写Object类的toString方法,实现按照题目要求的输出格式 */
    @Override
    public String toString() {
        return id + " " + total;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        Student[] students = new Student[n];
        int id;
        int chs;
        int math;
        int en;
        for (int i = 0; i < n; i++) {
            id = i + 1;
            chs = input.nextInt();
            math = input.nextInt();
            en = input.nextInt();
            students[i] = new Student(id, chs, math, en);
        }
        Arrays.sort(students);
        for (int i = 0; i < 5; i++) {
            System.out.println(students[i].toString());
        }
    }
}

五、C++程序

#include <iostream>
#include <algorithm>
using namespace std;

struct student // 定义student结构体,用于描述学生信息
{
    int id; // 标记学号
    int chs; // 标记语文成绩
    int math; // 标记数学成绩
    int en; // 标记英语成绩
    int total; // 标记三门课程的总分
};

int cmp(student x, student y) // 自定义排序规则函数
{
    if (x.total != y.total) // 如果两个学生的总分不相等
    {
        return x.total > y.total; // 则按照总分降序排序
    }
    else // 否则,两个学生的总分相等
    {
        if (x.chs != y.chs) // 如果两个学生的语文成绩不相等
        {
            return x.chs > y.chs; // 则按照语文成绩降序排序
        }
        else // 否则,两个学生的语文成绩相等
        {
            return x.id < y.id; // 则按照学号升序排序
        }
    }
}

int main()
{
    int n; // 学生人数
    cin >> n;
    student stu[n]; // 结构体数组,存储所有的学生信息
    int i;
    /* 输入每个学生的信息 */
    for (i = 0; i < n; i++)
    {
        stu[i].id = i + 1;
        cin >> stu[i].chs;
        cin >> stu[i].math;
        cin >> stu[i].en;
        stu[i].total = stu[i].chs + stu[i].math + stu[i].en; // 计算三门总分
    }
    sort(stu, stu + n, cmp); // 利用自定义排序规则函数对stu数组排序
    /* 按照题目输出要求,输出排序后的结果 */
    for (i = 0; i < 5; i++)
    {
        cout << stu[i].id << " " << stu[i].total << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江苏科技大学_计算机学院_潘磊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值