PAT 1080 MOOC期终成绩 (25 分) Java实现

该博客讨论了Java程序在处理时间限制内的性能问题,特别是在算法竞赛中的应用。作者指出,对于特定的时间限制,使用Java可能无法获得满分。博客通过示例展示了如何读取和处理学生数据,包括成绩计算和排序,强调了在时间和空间复杂度上的考虑。
摘要由CSDN通过智能技术生成

每次做到25分的题目时,都会不自觉看一眼时间限制。。。
time <= 200ms时用java大概率是得不了满分了
time <= 150ms时谨慎操作
time <= 100ms时直接放弃吧,换语言- -

在这里插入图片描述

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.*;

/**
 * @date 2021-9-5 - 22:29
 * Created by Salmon
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] strings = bufferedReader.readLine().split("\\s+");
        int n1 = Integer.parseInt(strings[0]);
        int n2 = Integer.parseInt(strings[1]);
        int n3 = Integer.parseInt(strings[2]);
        HashMap<String,Student> hashMap = new HashMap<>();
        for (int i = 0; i < n1; i++) {
            String[] strings1 = bufferedReader.readLine().split("\\s+");
            Student student = new Student();
            student.studyNum = strings1[0];
            student.Gp += Integer.parseInt(strings1[1]);
            hashMap.put(strings1[0],student);
        }
        for (int i = 0; i < n2; i++) {
            String[] strings2 = bufferedReader.readLine().split("\\s+");
            if(hashMap.containsKey(strings2[0])){
                Student student = hashMap.get(strings2[0]);
                student.Gmid_term += Integer.parseInt(strings2[1]);
                hashMap.put(strings2[0],student);
            }else {
                Student student = new Student();
                student.studyNum = strings2[0];
                student.Gmid_term += Integer.parseInt(strings2[1]);
                hashMap.put(strings2[0],student);
            }

        }
        for (int i = 0; i < n3; i++) {
            String[] strings3 = bufferedReader.readLine().split("\\s+");
            if(hashMap.containsKey(strings3[0])){
                Student student = hashMap.get(strings3[0]);
                student.Gfinal += Integer.parseInt(strings3[1]);
                hashMap.put(strings3[0],student);
            }else {
                Student student = new Student();
                student.studyNum = strings3[0];
                student.Gfinal += Integer.parseInt(strings3[1]);
                hashMap.put(strings3[0],student);
            }
        }
        bufferedReader.close();
        List<Student> list = new ArrayList<>();
        for (String key : hashMap.keySet()){
            list.add(hashMap.get(key));
        }

        List<Student> res = new ArrayList<>();
        for (Student student : list){
            if(student.Gp < 200){
                continue;
            }else if(student.Gmid_term > student.Gfinal){
                student.G = BigDecimal.valueOf(student.Gmid_term * 0.4 + student.Gfinal * 0.6).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
            }else {
                student.G = student.Gfinal;
            }

            if (student.Gmid_term == 0){
                student.Gmid_term = -1;
            }

            if(student.G >= 60){
                res.add(student);
            }
        }

        Collections.sort(res, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if(o2.G - o1.G > 0){
                    return 1;
                }else if(o2.G - o1.G < 0){
                    return -1;
                }else {
                    int n1 = 0;
                    int n2 = 0;
                    for (int i = 0; i < o1.studyNum.length(); i++) {
                        n1 += o1.studyNum.charAt(i) - '0';
                    }
                    for (int i = 0; i < o2.studyNum.length(); i++) {
                        n2 += o2.studyNum.charAt(i) - '0';
                    }
                    return Integer.compare(n1, n2);
                }
            }
        });

        for (Student student : res) {
            System.out.println(student);
        }


    }

    static class Student{
        String studyNum;
        int Gp = 0;
        int Gmid_term = 0;
        int Gfinal = 0;
        int G = 0;

        @Override
        public String toString() {
            return studyNum + " " + Gp + " " + Gmid_term + " " + Gfinal + " " + G;
        }
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值