华为OD机试真题 - 智能成绩表

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] nums = Arrays.stream(in.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        int studentCount = nums[0];
        int subjectCount = nums[1];
        String[][] map = new String[studentCount + 1][subjectCount + 1];
        for (int i = 0; i < studentCount + 1; i++) {
            if (i == 0) {
                String[] subjects = in.nextLine().split(&quot; &quot;);
                for (int j = 0; j < subjectCount; j++) {
                    map[i][j + 1] = subjects[j];
                }
            } else {
                String[] student = in.nextLine().split(&quot; &quot;);
                for (int j = 0; j < student.length; j++) {
                    map[i][j] = student[j];
                }
            }

        }
        String sub = in.nextLine();
        String[][] map1 = getRanks(sub, map);
        for (int i = 0; i < map1.length; i++) {
            System.out.print(map1[i][0] + &quot; &quot;);
        }
    }

    public static String[][] getRanks(String subjects, String[][] map) {
        int sub = -1;
        for (int i = 1; i < map[0].length; i++) {
            if (map[0][i].equals(subjects))
                sub = i;
        }
        int finalSub = sub;
        String[][] map1 = new String[map.length - 1][map[0].length];
        for (int i = 1; i < map.length; i++) {
            map1[i - 1] = map[i];
        }
        Arrays.sort(map1, new Comparator<String[]>() {
            @Override
            public int compare(String[] o1, String[] o2) {
                if (finalSub != -1)
                    return Integer.parseInt(o2[finalSub]) - Integer.parseInt(o1[finalSub]);
                else {
                    int o1Sum = 0;
                    int o2Sum = 0;
                    for (int i = 1; i < o1.length; i++) {
                        o1Sum += Integer.parseInt(o1[i]);
                        o2Sum += Integer.parseInt(o2[i]);
                    }
                    return o1Sum - o2Sum;
                }
            }
        });
        return map1;
    }
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务