20230923 小米笔试AK

先mark一下,笔试结束后发题解

===============================

t1:信号站求一个信号最大的点位

解法:维护一个101*101大小的mp,存储信号的强度,然后遍历每个信号站的radius范围内的格子,累加到mp中

import java.util.Scanner;

public class t1 {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        String[] s = sc.nextLine().split(",");
        int n = Integer.parseInt(s[0]);
        int radius = Integer.parseInt(s[1]);
        int[][] mp = new int[101][101];
        for (int i = 0; i < n; ++i) {
            s = sc.nextLine().split(",");
            int xx = 0;
            int yy = 0;
            int pw = 0;
            try {
                xx = Integer.parseInt(s[0]);
                yy = Integer.parseInt(s[1]);
                pw = Integer.parseInt(s[2]);
            } catch (Exception e) {
                //非常奇怪,必须捕获异常,否则会只过87%
            }
            for (int x = Math.max(0, xx - radius); x <= Math.min(100, xx + radius); ++x) {
                for (int y = Math.max(0, yy - radius); y <= Math.min(100, yy + radius); ++y) {
                    int d = (x - xx) * (x - xx) + (y - yy) * (y - yy);
                    if (d > radius * radius) continue;
                    double d1 = Math.sqrt(d * 1.0);
                    mp[x][y] += (int) Math.floor((pw / (1 + d1)));
                }
            }
        }
        int x0 = 0;
        int y0 = 0;
        int maxm = 0;
        for (int i = 0; i <= 100; ++i) {
            for (int j = 0; j <= 100; ++j) {
                if (maxm < mp[i][j]) {
                    maxm = mp[i][j];
                    x0 = i;
                    y0 = j;
                }
            }
        }
        System.out.println(x0 + "," + y0);
    }
}

t2 判断工序是否合理

解法:拓扑排序

import java.util.*;

public class t2 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        String[] s = sc.nextLine().split(",");
        LinkedList<Integer>[] mp = new LinkedList[n];
        for (int i = 0; i < n; ++i) {
            mp[i] = new LinkedList<>();
        }
        int[] in = new int[n];
        for (int i = 0; i < s.length; ++i) {
            String[] ss = s[i].split(":");
            int a = Integer.parseInt(ss[0]);
            int b = Integer.parseInt(ss[1]);
            mp[b].add(a);
            in[a]++;
        }
        LinkedList<Integer> que = new LinkedList<>();
        for (int i = 0; i < n; ++i) {
            if (in[i]== 0) {
                que.add(i);
            }
        }
        while (!que.isEmpty()) {
            int first = que.pollFirst();
            for (Integer next : mp[first]) {
                in[next]--;
                if (in[next] == 0) {
                    que.add(next);
                }
            }
        }
        int flag = 1;
        for (int i = 0; i < n; ++i) {
            if (in[i] != 0) {
                flag = 0;
                break;
            }
        }
        System.out.println(flag);
    }
}

全部评论
第二题拓扑排序为啥一直只有67%
1 回复 分享
发布于 2023-09-23 17:31 浙江
67,100,第一题基站没AK,有点难受
点赞 回复 分享
发布于 2023-09-23 17:35 新加坡
有算法方向的同学第二题是1到26编码a到z的吗?我一直80%,是不是数据有问题
点赞 回复 分享
发布于 2023-09-23 17:35 北京
89,100,第一题基站没AK,有点难受
点赞 回复 分享
发布于 2023-09-23 17:32 四川
1.78/2,第二题思路有点问题
点赞 回复 分享
发布于 2023-09-23 17:31 浙江
关注了,是大佬,好多笔试都ak了
点赞 回复 分享
发布于 2023-09-23 17:26 江苏
怎么什么都在ak啊,佬😭
点赞 回复 分享
发布于 2023-09-23 17:24 浙江
m
点赞 回复 分享
发布于 2023-09-23 17:24 湖北
我做的第二道是课程表
点赞 回复 分享
发布于 2023-09-23 17:23 江苏

相关推荐

04-16 19:19
已编辑
合肥大学 Java
刷了100道题的大老虎很想提桶:27届现在早没日常hc了,不可能找到的,等暑假9月吧
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
5
14
分享

创作者周榜

更多
牛客网
牛客企业服务