Java算法之找出作弊的人

题目描述

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

过滤的规则为:找到分差最小的员工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,因此最终结果为

 

样例2

输入:

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

1 2
2 3
4 5 
 

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class FindCheaters找出作弊者 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        Pair[] pairs = new Pair[n];
        int minDiff = 300;
        for (int i = 0; i < n; i++) {
            int id = scanner.nextInt();
            int score = scanner.nextInt();
            pairs[i] = new Pair(id, score);
            if (i > 0 && Math.abs(pairs[i].score - pairs[i - 1].score) < minDiff) {
                minDiff = Math.abs(pairs[i].score - pairs[i - 1].score);
            }
        }
        scanner.close();

        // 存储分差最小的员工ID对
        List<Pair> minDiffPairs = new ArrayList<>();
        for (int i = 1; i < n; i++) {
            if (Math.abs(pairs[i].score - pairs[i - 1].score) == minDiff) {
                minDiffPairs.add(pairs[i - 1]);
            }
        }

        // 排序
        Collections.sort(minDiffPairs);

        // 打印结果
        for (Pair pair : minDiffPairs) {
            System.out.println(pair.id + " " + (pair.id + 1));
        }
    }
}

class Pair implements Comparable<Pair> {
    int id;
    int score;

    Pair(int id, int score) {
        this.id = id;
        this.score = score;
    }

    // 重写compareTo方法,用于排序
    @Override
    public int compareTo(Pair other) {
        return this.id - other.id;
    }
}

 

  • 20
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值