【exam answer 1】

1
2
3
4
题目描述:
输入的第一行为n(n<40),表示有n个同学,接下来的n行每行有4个输入,分别为该学生的名字,语、数、外成绩,请按照排序规则对学生进行排序,规则如下:
1、总成绩高的排在前面 <br>2、总成绩相同的情况下,语文成绩高的排在前面。 <br>3、在总成绩,语文成绩都相同的情况下,数学成绩高的排在前面。
4、在成绩都相同的情况下,先输入的同学排在前面。<br><br>参考答案:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package package01;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
 
/**
 * 题目描述:
 * 输入的第一行为n(n<40),表示有n个同学,接下来的n行每行有4个输入,分别为该学生的名字,语、数、外成绩,请按照排序规则对学生进行排序,规则如下:
 * 1、总成绩高的排在前面 <br> * 2、总成绩相同的情况下,语文成绩高的排在前面。 <br> * 3、在总成绩,语文成绩都相同的情况下,数学成绩高的排在前面。
 * 4、在成绩都相同的情况下,先输入的同学排在前面。
 *
 * 例如依次输入:
 * 3
 * aa 70 80 75
 * bb 80 85 90
 * cc 60 65 88
 *
 */
public class Main {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int input = sc.nextInt();
 
        func(input, sc);
 
    }
 
    public static void func(int input, Scanner sc) {
        if (input >= 1 && input <= 40) {
            String[] arr = new String[input + 1];
            TreeMap<String, Integer> map = new TreeMap<>();
            for (int i = 0; i < input + 1; i++) {
                arr[i] = sc.nextLine();
            }
 
            for (int i = 1; i < arr.length; i++) {
                int chinese = Integer.parseInt(arr[i].split(" ")[1]);
                int math = Integer.parseInt(arr[i].split(" ")[2]);
                int english = Integer.parseInt(arr[i].split(" ")[3]);
                int totalScore = chinese + math + english;
                map.put(i + "," + arr[i], totalScore);
            }
 
            List<Entry<String, Integer>> arrayList = new ArrayList<>(map.entrySet());
 
            Collections.sort(arrayList, new Comparator<Map.Entry<String, Integer>>() {
                @Override
                public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
                    // 降序
                    if (o2.getValue().compareTo(o1.getValue()) != 0) {
                        return o2.getValue().compareTo(o1.getValue());
                    } else {
                        int chinese1 = Integer.parseInt(o1.getKey().split(" ")[1]);
                        int chinese2 = Integer.parseInt(o2.getKey().split(" ")[1]);
                        if (chinese2 < chinese1) {
                            return -1;
                        } else if (chinese1 > chinese2) {
                            return 1;
                        } else {
                            int math1 = Integer.parseInt(o1.getKey().split(" ")[2]);
                            int math2 = Integer.parseInt(o2.getKey().split(" ")[2]);
                            if (math2 < math1) {
                                return -1;
                            } else if (math1 > math2) {
                                return 1;
                            } else {
                                int english1 = Integer.parseInt(o1.getKey().split(" ")[3]);
                                int english2 = Integer.parseInt(o2.getKey().split(" ")[3]);
                                if (english2 < english1) {
                                    return -1;
                                } else if (english2 > english1) {
                                    return 1;
                                } else {
                                    int p1 = Integer.parseInt(o1.getKey().split(",")[0]);
                                    int p2 = Integer.parseInt(o2.getKey().split(",")[0]);
                                    if (p2 < p1) {
                                        return -1;
                                    } else {
                                        return 1;
                                    }
                                }
 
                            }
                        }
                    }
                }
            });
 
            for (Entry<String, Integer> tmp : arrayList) {
                System.out.println(tmp.getKey().split(",")[1]);
            }
        } else {
            System.err.println("输入的同学个数有误,请重新输入!");
 
            sc = new Scanner(System.in);
            input = sc.nextInt();
 
            func(input, sc);
        }
 
    }
 
}

  

posted @   尐鱼儿  阅读(132)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示