小马智行 8.28 java笔试 第二题AC代码

其它题目没有AC就不贴了
2.
import java.util.Arrays;
import java.util.Scanner;
/*
100%
 */
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int k = in.nextInt();
        int[] nums = new int[m];
        for (int i = 0; i < m; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
            if (y == 0) {
                nums[i] = x;
            }
            else if (x == n) {
                nums[i] = x + y;
            }
            else if (y == n) {
                nums[i] = (n - x) + 2 * n;
            }
            else if (x == 0) {
                nums[i] = (n - y) + 3 * n;
            }
        }
        Arrays.sort(nums);

        int res = Integer.MAX_VALUE;
        k--;

        for (int i = 0; i < nums.length; i++) {
            if (i + k < nums.length) {
                res = Math.min(nums[i + k] - nums[i], res);
            }
            else {
                res = Math.min(4 * n - nums[i] + nums[(i + k) % nums.length], res);
            }
        }

        System.out.println(res);
    }
}


#小马智行##笔试题目#
全部评论
我真是醉了,全写对了,但是n写成了5,一直没意识到错在哪儿,裂开了
1 回复 分享
发布于 2021-08-28 21:45

相关推荐

好在哪里了?我请问了?
仁者伍敌:活着的人都说好,帮我盖上棺材盖谢谢
点赞 评论 收藏
分享
程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
05-05 21:45
已编辑
广州大学 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
今天 18:18
点赞 评论 收藏
分享
评论
2
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务