最小分差对。公司组织了一次考试,现在考试结果出来了,想看一下有没人存在作弊行为,但是员工太多了,需要先对员工进行一次过滤,再进一步确定是否存在作弊行为。

题目描述
公司组织了一次考试,现在考试结果出来了,想看一下有没人存在作弊行为,但是员工太多了,需要先对员工进行一次过滤,再进一步确定是否存在作弊行为。

过滤的规则为:找到分差最小的员工ID对(p1,p2)列表,要求p1<p2

员工个数取值范国:O<n<100000

员工ID为整数,取值范围:0<=n<=100000

考试成绩为整数,取值范围:0<=score<=300

输入描述
员工的ID及考试分数

输出描述
分差最小的员工ID对(p1,p2)列表,要求p1<p2。每一行代表一个集合,每个集合内的员工ID按顺序排列,多行结果也以员工对中p1值大小升序排列(如果p1相同则p2升序)。

样例1
输入:

5
1 90
2 91
3 95
4 96
5 100
输出:

1 2
3 4
解释:

输入:第一行为员工个数n,后续的n行第一个数值为员工ID,第二个数值为员工考试分数

输出:员工1和员工2的分差为1,员工3和员工4的分差也为1,因此最终结果为

1 2

3 4

样例2
输入:

5
1 90
2 91
3 92
4 85
5 86
输出:

1 2
2 3
4 5
解题思路
public static void score() {

Scanner s = new Scanner(System.in);
int n = s.nextInt();
int nn = n;
HashMap<Integer, String[]> m = new HashMap<>();

List<String[]> l = new ArrayList<>();
while (nn > 0) {
    String s1 = s.nextLine();
    if ("".equals(s1)) {
        continue;
    }
    String[] ns = s1.split("\\s+");
    l.add(ns);
    nn--;
}

Map<Integer, List<List<Integer>>> mm = new HashMap<>();
int minCha = 300;
for (int i = 0; i < l.size() - 1; i++) {
    String[] tem1 = l.get(i);
    for (int j = i + 1; j < l.size(); j++) {
        String[] tem2 = l.get(j);
        // 计算分差,保存 排序后的学生编号
        int num1 = Integer.parseInt(tem1[0]);
        int num2 = Integer.parseInt(tem2[0]);

        int score1 = Integer.parseInt(tem1[1]);
        int score2 = Integer.parseInt(tem2[1]);
        int cha = Math.abs(score2 - score1);
        minCha = Math.min(minCha, cha);
        List<List<Integer>> lists = mm.get(cha);
        if (lists == null) {
            lists = new ArrayList<>();
        }
        if (num1 > num2) {
            lists.add(Arrays.asList(num2, num1));
        } else {
            lists.add(Arrays.asList(num1, num2));
        }
        mm.put(cha, lists);
    }
}

List<List<Integer>> lists = mm.get(minCha);
for (int i = 0; i < lists.size(); i++) {
    List<Integer> integers = lists.get(i);
    Collections.sort(integers);
    for (int j = 0; j < integers.size(); j++) {
        System.out.print(integers.get(j) + ((j == 1) ? "" : " "));
    }
    System.out.println();
}

}

posted @   vello  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示