华为OD真题--选修课--带答案

2023华为OD统一考试(A+B卷)题库清单-带答案(持续更新)or2023年华为OD真题机考题库大全-带答案(持续更新)

项目描述

现有两门选修课,每门选修课都有一部分学生选修,每个学生都有选修课的成绩,需要你找出同时选修了两门选修课的学生,先按照班级进行划分,班级编号小的先输出,每个班级按照两门选修课成绩和的降序排序,成绩相同时按照学生的学号升序排序。

输入描述

第一行为第一门选修课学生的成绩

第二行为第二门选修课学生的成绩,每行数据中学生之间以英文分号分隔,每个学生的学号和成绩以英文逗号分隔,学生学号的格式为8位数字(2位院系编号+入学年份后2位+院系内部1位专业编号+所在班级3位学号),学生成绩的取值范围为[0,100]之间的整数,两门选修课选修学生数的取值范围为[1-2000]之间的整数。

输出描述

同时选修了两门选修课的学生的学号,如果没有同时选修两门选修课的学生输出NULL,否则,先按照班级划分,班级编号小的先输出,每个班级先输出班级编号(学号前五位),然后另起一行输出这个班级同时选修两门选修课的学生学号,学号按照要求排序(按照两门选修课成绩和的降序,成绩和相同时按照学号升序),学生之间以英文分号分隔。

示例1

输入:

01202021,75;01201033,95;01202008,80;01203006,90;01203088,100

01202008,70;01203088,85;01202111,80;01202021,75;01201100,88

输出:

01202

01202008;01202021

01203

01203088

说明:

同时选修了两门选修课的学生01202021、01202008、01203088,这三个学生两门选修课的成绩和分别为150、150、185, 01202021、01202008属于01202班的学生,按照成绩和降序,成绩相同时按学号升序输出的结果为01202008:01202021,01203088属于01203班的学生,按照成绩和降序,成绩相同时按学号升序输出的结果为01203088,01202的班级编号小于01203的班级编号,需要先输出。

示例2

输入:

01201022,75;01202033,95;01202018,80;01203006,90;01202066,100

01202008,70;01203102,85;01202111,80;01201021,75;01201100,88

输出:

NULL

说明:

没有同时选修了两门选修课的学生,输出NULL。

java 复制代码
public class OptionalCourse {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] one = sc.nextLine().split(";");
        String [] two = sc.nextLine().split(";");
        student(one,two);
    }

    public static void student(String[] one,String [] two){
        List<StudentInfo> oneStudentInfos = new ArrayList<>();
        List<StudentInfo> twoStudentInfos = new ArrayList<>();
        List<StudentInfo> endStudentInfos = new ArrayList<>();
        Set<String> schoolInfo = new HashSet<>();
        //2门成绩的id跟分数都存在List对象中
        for (String s : one){
            String [] one1 = s.split(",");
            StudentInfo st = new StudentInfo(one1[0],Integer.valueOf(one1[1]));
            oneStudentInfos.add(st);
        }
        for (String s : two){
            String [] two2 = s.split(",");
            StudentInfo st = new StudentInfo(two2[0],Integer.valueOf(two2[1]));
            twoStudentInfos.add(st);
        }
        //将2门都存在成绩的学生信息统计
        for (int i = 0; i < oneStudentInfos.size(); i++){
            for (int j = 0; j < twoStudentInfos.size(); j++){
                if (oneStudentInfos.get(i).id.equals(twoStudentInfos.get(j).id)){
                    int score = oneStudentInfos.get(i).score + twoStudentInfos.get(i).score;
                    StudentInfo st = new StudentInfo(oneStudentInfos.get(i).id,score);
                    endStudentInfos.add(st);
                    //截取开头5位数
                    String schoolId = oneStudentInfos.get(i).id.substring(0,5);
                    schoolInfo.add(schoolId);
                }
            }
        }
        if (endStudentInfos.size() == 0){
            System.out.println("NULL");
            return;
        }
        //将set开头5位数排序后存在List中
        List<String> schoolInfos = schoolInfo.stream()
                .sorted(Comparator.naturalOrder())
                .collect(Collectors.toList());
        compareStudent(endStudentInfos,schoolInfos);
    }

    /**
     * 按照存储的开头5位数进行分类输出
     * @param endStudentInfos
     * @param schoolInfo
     */
    public static void compareStudent(List<StudentInfo> endStudentInfos,List<String> schoolInfo){
        Collections.sort(endStudentInfos,new StudentInfo());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < schoolInfo.size();i++){
            System.out.println(schoolInfo.get(i));
            for (int j = 0; j < endStudentInfos.size(); j++){
                if (endStudentInfos.get(j).id.substring(0,5).equals(schoolInfo.get(i))){
                   sb.append(endStudentInfos.get(j).id).append(";");
                }
            }
            System.out.println(sb.toString().substring(0,sb.length()-1));
            sb.setLength(0);
        }
    }


    @Data
    static class StudentInfo implements Comparator<StudentInfo> {
        String id;
        int score;

        public StudentInfo(String id, int score) {
            this.id = id;
            this.score = score;
        }

        public StudentInfo() {
        }

        @Override
        public int compare(StudentInfo o1, StudentInfo o2) {
            return Integer.valueOf(o1.id) - Integer.valueOf(o2.id);
        }
    }
}
相关推荐
荔枝吻几秒前
【沉浸式解决问题】idea开发中mapper类中突然找不到对应实体类
java·intellij-idea·mybatis
刘海东刘海东4 分钟前
结构型智能科技的关键可行性——信息型智能向结构型智能的转变(修改提纲)
人工智能·算法·机器学习
snoopyfly~16 分钟前
Ubuntu 24.04 LTS 服务器配置:安装 JDK、Nginx、Redis。
java·服务器·ubuntu
pumpkin8451430 分钟前
Rust 调用 C 函数的 FFI
c语言·算法·rust
挺菜的42 分钟前
【算法刷题记录(简单题)003】统计大写字母个数(java代码实现)
java·数据结构·算法
mit6.82442 分钟前
7.6 优先队列| dijkstra | hash | rust
算法
2401_858286111 小时前
125.【C语言】数据结构之归并排序递归解法
c语言·开发语言·数据结构·算法·排序算法·归并排序
掘金-我是哪吒1 小时前
分布式微服务系统架构第156集:JavaPlus技术文档平台日更-Java线程池使用指南
java·分布式·微服务·云原生·架构
亲爱的非洲野猪2 小时前
Kafka消息积压的多维度解决方案:超越简单扩容的完整策略
java·分布式·中间件·kafka
wfsm2 小时前
spring事件使用
java·后端·spring