PAT 甲级 1036 JAVA

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Main {
    public static void main (String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        Student[] students = new Student[n];
        for (int i = 0; i < n; i++) {
            String[] spl = br.readLine().split(" ");
            students[i] = new Student(spl[0], spl[1], spl[2], Integer.parseInt(spl[3]));
        }
        List<Student> students_m = new ArrayList<>();
        List<Student> students_f = new ArrayList<>();
        for (Student s : students) {
            if (s.sex.equals("M")) {
                students_m.add(s);
            } else {
                students_f.add(s);
            }
        }
        Collections.sort(students_f, new ComparatorF());
        Collections.sort(students_m, new ComparatorM());
        if (students_f.size() == 0) System.out.println("Absent");
        else System.out.println(students_f.get(0).name + " " + students_f.get(0).course_id);
        if (students_m.size() == 0) System.out.println("Absent");
        else System.out.println(students_m.get(0).name + " " + students_m.get(0).course_id);
        if (students_f.size() == 0 || students_m.size() == 0) System.out.println("NA");
        else System.out.println(students_f.get(0).score - students_m.get(0).score);
    }

    private static class Student {
        String name;
        String sex;
        String course_id;
        int score;

        Student (String name, String sex, String course_id, int score) {
            this.name = name;
            this.sex = sex;
            this.course_id = course_id;
            this.score = score;
        }
    }

    private static class ComparatorM implements Comparator<Student> {

        @Override
        public int compare(Student o1, Student o2) {
            return o1.score - o2.score;
        }
    }

    private static class ComparatorF implements Comparator<Student> {

        @Override
        public int compare(Student o1, Student o2) {
            return o2.score - o1.score;
        }
    }
}

AC

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值