【PAT】1028.List Sorting

【PAT】1028.List Sorting

Excel can sort records according to any column. Now you are supposed to imitate this function.

Input Specification:
Each input file contains one test case. For each case, the first line contains two integers N (≤105) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student’s record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).

Output Specification:
For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID’s; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID’s in increasing order.

Sample Input 1:

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

Sample Output 1:

000001 Zoe 60
000007 James 85
000010 Amy 90

Sample Input 2:

4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98

Sample Output 2:

000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60

Sample Input 3:

4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 9

Sample Output 3:

000002 James 9
000001 Zoe 60
000007 James 85
000010 Amy 90

题解

根据C的值是1还是2还是3,对相应的列排序。第一列(学号)升序,第二列(姓名)不降序,第三列(成绩)不降序。注:姓名或者成绩相同时根据学号升序排序。

import java.util.*;

public class Solution2 {
    //学生结构体
    public static class Student {
        int id;
        String name;
        int grade;

        Student(int id, String name, int grade) {
            this.id = id;
            this.name = name;
            this.grade = grade;
        }
    }

    public static void main(String[] args) {
        List<Student> Stulist = new ArrayList<>();
        int N, C;
        Scanner in = new Scanner(System.in);
        N = in.nextInt();
        C = in.nextInt();

        //输入学生信息
        for (int i = 0; i < N; i++) {
            int id = in.nextInt();
            String name = in.next();
            int grade = in.nextInt();

            Stulist.add(new Student(id, name, grade));
        }

        //根据C 的三种不同排序方法
        if (C == 1) {
            sort(1, Stulist);
        }
        else if (C == 2) {
            sort(2, Stulist);
        }
        else {
            sort(3, Stulist);
        }

        //输出排序后的的List
        for (int i = 0; i < N; i++) {
            Student stu = Stulist.get(i);
            //按格式输出
            System.out.printf("%06d %s %d", stu.id, stu.name, stu.grade);
            System.out.println();
        }
    }

    public static void sort(int C, List<Student> list) {
        if (C == 1) {
            Collections.sort(list, new comparator1());
        }
        else if (C == 2) {
            Collections.sort(list, new comparator2());
        }
        else {
            Collections.sort(list, new comparator3());
        }
    }

    //return 1:交换位置
    //return -1:不交换位置
    // return o1-o2:升序排列
    public static class comparator1 implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            return (o1.id - o2.id) <= 0 ? -1 : 1;
        }
    }

    public static class comparator2 implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            if (o1.name.equals(o2.name)) {
                return (o1.id - o2.id) <= 0 ? -1 : 1;
            }
            return o1.name.compareTo(o2.name);
        }
    }

    public static class comparator3 implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            if (o1.grade == o2.grade) {
                return (o1.id - o2.id) <= 0 ? -1 : 1;
            }
            return (o1.grade - o2.grade <= 0) ? -1 : 1;
        }
    }
}

结果

测试点 提示 内存(KB) 用时(ms) 结果 得分
0 17168 115
答案正确
5 / 5
1 16476 118
答案正确
5 / 5
2 16564 118
答案正确
5 / 5
3 16972 118
答案正确
2 / 2
4 16916 113
答案正确
2 / 2
5 16744 116
答案正确
2 / 2
6 51104 400
运行超时
0 / 4
部分正确 分数 21 / 25,有一个测试点超时。

你可能感兴趣的:(Java相关,#,力扣及OJ,java,算法)