zoukankan      html  css  js  c++  java
  • Java实现 蓝桥杯 算法提高 成绩排序2

    试题 算法提高 成绩排序2

    资源限制
    时间限制:1.0s 内存限制:256.0MB
    问题描述
      给出n个学生的成绩,将这些学生按成绩排序,排序规则:总分高的在前;总分相同,数学成绩高的在前;总分与数学相同,英语高的在前;总分数学英语都相同,学号小的在前
    输入格式
      第一行一个正整数n,表示学生人数
      接下来n行每行3个0~100的整数,第i行表示学号为i的学生的数学、英语、语文成绩
    输出格式
      输出n行,每行表示一个学生的数学成绩、英语成绩、语文成绩、学号
      按排序后的顺序输出
    样例输入
    2
    1 2 3
    2 3 4
    样例输出
    2 3 4 2
    1 2 3 1
    数据规模和约定
      n≤100

    package com.company;
    
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class 成绩排序2 {
        public static class Students implements Comparable {
            int Math;
            int English;
            int Chinese;
            int Num;
            int Sum;
    
            @Override
            public String toString() {
                return this.Math + " " + this.English + " " + this.Chinese + " " + this.Num;
            }
    
            @Override
            public int compareTo(Object o) {
                Students s = (Students) o;
                if (this.Sum > s.Sum) {
                    return 1;
                } else if (this.Sum == s.Sum && this.Math > s.Math) {
                    return 1;
                } else if (this.Sum == s.Sum && this.Math == s.Math && this.English > s.English) {
                    return 1;
                } else if (this.Math == s.Math && this.English == s.English && this.Sum == s.Sum && this.Num < s.Num) {
                    return 1;
                }
                return -1;
            }
        }
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            Students[] students = new Students[n];
            for (int i = 0; i < n; i++) {
                Students stu = new Students();
                stu.Math = sc.nextInt();
                stu.English = sc.nextInt();
                stu.Chinese = sc.nextInt();
                stu.Num = i + 1;
                stu.Sum = stu.Math + stu.English + stu.Chinese;
                students[i] = stu;
            }
            Arrays.sort(students);
            for (int i = n - 1; i >= 0; i--)
                System.out.println(students[i].toString());
    
        }
    }
    
    
  • 相关阅读:
    Linux 使用Crontab设置定时调用Shell文件
    Oracle SqlDeveloper创建JOB
    数据仓库搭建步骤
    Linux学习内容
    Windows平台手动卸载Oracle Server【完整+干净】
    ORA-12638:身份证明检索失败
    成功数据迁移的详细步骤
    SQL Server 日期转换到字符串
    删除无用文件,清理硬盘
    KMS安装后激活机器
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12946938.html
Copyright © 2011-2022 走看看