我是靠谱客的博主 忧虑服饰,最近开发中收集的这篇文章主要介绍PAT 甲级 1028 JAVA,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {

    public static void main (String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] NandC = br.readLine().split(" ");
        int N = Integer.parseInt(NandC[0]);
        int C = Integer.parseInt(NandC[1]);
        Student[] stus = new Student[N];
        for (int i = 0; i < N; i++) {
            String[] info = br.readLine().split(" ");
            stus[i] = new Student();
            stus[i].ID = info[0];
            stus[i].name = info[1];
            stus[i].score = Integer.parseInt(info[2]);
        }
        Comparator1 com1 = new Comparator1();
        Comparator2 com2 = new Comparator2();
        Comparator3 com3 = new Comparator3();
        if (C == 1) {
            Arrays.sort(stus, com1);
        } else if (C == 2) {
            Arrays.sort(stus, com2);
        } else {
            Arrays.sort(stus, com3);
        }
        for (Student s : stus) {
            System.out.println(s.ID + " " + s.name + " " + s.score);
        }
    }

    private static class Student {
        String ID;
        String name;
        int score;
    }

    public static class Comparator1 implements Comparator<Student> {
        @Override
        public int compare (Student s1, Student s2) {
            return s1.ID.compareTo(s2.ID);
        }
    }

    public static class Comparator2 implements Comparator<Student> {
        @Override
        public int compare (Student s1, Student s2) {
            if (s1.name.compareTo(s2.name) < 0) {
                return -1;
            } else if (s1.name.compareTo(s2.name) > 0) {
                return 1;
            } else {
                return s1.ID.compareTo(s2.ID);
            }
        }
    }

    public static class Comparator3 implements Comparator<Student> {
        @Override
        public int compare (Student s1, Student s2) {
            if (s1.score - s2.score < 0) {
                return -1;
            } else if (s1.score - s2.score > 0) {
                return 1;
            } else {
                return s1.ID.compareTo(s2.ID);
            }
        }
    }
}

最后一个测试用例超时,原因未知

最后

以上就是忧虑服饰为你收集整理的PAT 甲级 1028 JAVA的全部内容,希望文章能够帮你解决PAT 甲级 1028 JAVA所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部