0812美团笔试AK代码

Q1

import java.util.Scanner;

public class P1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) nums[i] = scan.nextInt();
        int x = scan.nextInt(), y = scan.nextInt();
        boolean flag = false;
        for (int i = 0; i < n; i++) {
            if (i > 0) {
                if ((nums[i] == x && nums[i - 1] == y) || (nums[i] == y && nums[i - 1] == x)) {
                    flag = true;
                    break;
                }
            }
            if (i < n - 1) {
                if ((nums[i] == x && nums[i + 1] == y) || (nums[i] == y && nums[i + 1] == x)) {
                    flag = true;
                    break;
                }
            }
        }
        System.out.println(flag ? "Yes" : "No");
    }
}

Q2

import java.util.Scanner;

public class P2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) nums[i] = scan.nextInt();
        int x = scan.nextInt(), y = scan.nextInt();
        if (x > y) {
            int temp = x;
            x = y;
            y = temp;
        }
        long ans1 = 0, ans2 = 0;
        for (int i = x; i < y; i++) {
            ans1 += nums[i - 1];
        }
        for (int i = 0; i < x - 1; i++) {
            ans2 += nums[i];
        }
        for (int i = y - 1; i < n; i++) {
            ans2 += nums[i];
        }
        System.out.println(Math.min(ans1, ans2));
    }
}

Q3

import java.util.Scanner;

public class P3 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt(), m = scan.nextInt();
        long sum = 0, ans = Long.MAX_VALUE;
        long[] col = new long[m], row = new long[n];
        for (int i = 0; i < n; i++) {
            long s = 0;
            for (int j = 0; j < m; j++) {
                long t = scan.nextInt();
                sum += t;
                s += t;
                col[j] += t;
            }
            row[i] = s;
        }
        long t = 0;
        for (int i = 0; i < n; i++) {
            t += row[i];
            ans = Math.min(Math.abs(sum - t - t), ans);
        }
        t = 0;
        for (int i = 0; i < m; i++) {
            t += col[i];
            ans = Math.min(Math.abs(sum - t - t), ans);
        }
        System.out.println(ans);
    }
}

Q4

import java.util.Scanner;

public class P4 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        char[] cs = scan.next().toCharArray();
        long ans = n;
        for (int i = 1; i <= n / 2; i++) {
            if (n % i == 0) {
                char[][] g = new char[i][n / i];
                boolean[][] vis = new boolean[i][n / i];
                int a = 0, b = 0, t = 0;
                for (int j = 0; j < n; j++) {
                    g[b][a] = cs[j];
                    a++;
                    if (a == n / i) {
                        a = 0;
                        b++;
                    }
                }
                for (int x = 0; x < i; x++) {
                    for (int y = 0; y < n / i; ++y) {
                        if (!vis[x][y]) {
                            dfs(g, x, y, i, n / i, g[x][y], vis);
                            t++;
                        }
                    }
                }
                ans = Math.min(ans, t);
            }
        }
        System.out.println(ans);
    }

    public static void dfs(char[][] g, int i, int j, int x, int y, char c, boolean[][] vis) {
        if (i < 0 || j < 0 || i >= x || j >= y || vis[i][j] || g[i][j] != c) return;
        vis[i][j] = true;
        dfs(g, i + 1, j, x, y, c, vis);
        dfs(g, i - 1, j, x, y, c, vis);
        dfs(g, i, j + 1, x, y, c, vis);
        dfs(g, i, j - 1, x, y, c, vis);
    }
}

Q5

import java.util.*;

public class P5 {
    static int n;
    static int[] ws;
    static List<Integer>[] adjs;

    static int[][] dp; // dp[i][j] - 表示第i个节点为第j种颜色时的答案数

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        n = scan.nextInt();
        ws = new int[n];
        for (int i = 0; i < n; i++) ws[i] = scan.nextInt();
        adjs = new ArrayList[n];
        dp = new int[n][2];
        for (int i = 0; i < n; i++) adjs[i] = new ArrayList<>();
        for (int i = 0; i < n - 1; i++) {
            int u = scan.nextInt() - 1, v = scan.nextInt() - 1;
            adjs[u].add(v);
            adjs[v].add(u);
        }
        int ans = 0;
        dfs(0, -1);
        System.out.println(Math.max(dp[0][0], dp[0][1]));
    }

    public static void dfs(int u, int fa) {
        Set<Integer> vs = new HashSet<>();
        for (int v : adjs[u]) {
            if (v == fa) continue;
            dfs(v, u);
            dp[u][0] += Math.max(dp[v][1], dp[v][0]);
            if (check((long) ws[u] * ws[v])) vs.add(v);
        }
        for (int v : vs) {
            dp[u][1] = Math.max(dp[u][1], dp[u][0] - Math.max(dp[v][1], dp[v][0]) + dp[v][0] + 2); // 树的子节点互不影响
        }
    }

    public static boolean check(long x) {
        long t = (long) Math.sqrt(x);
        return t * t == x;
    }
}

#美团##美团笔试#
全部评论
哥们 拼多多实习还有时间做笔试的吗
点赞 回复 分享
发布于 2025-05-17 18:20 广东
tql
点赞 回复 分享
发布于 2023-08-16 01:42 上海
第三题可不可以只算大于等于sum二分之一的列或行啊,我这么算的只有60%
点赞 回复 分享
发布于 2023-08-12 14:52 北京

相关推荐

03-19 18:27
已编辑
门头沟学院 C++
26学院本太难了,很多公司机筛就给我刷了。机会都难拿到如果是简历存在问题也欢迎拷打————————————————————分割线——————————————————————2026.3.4更新:发完贴之后,时不时投递又收到了不少的笔试/面试邀请。主要是之前投递简历出去之后基本上都是沉默状态,年后好转了不少timeline:2026.01.21&nbsp;文远知行笔试,半年多没刷算法题&nbsp;-&gt;挂&nbsp;(后续HR说春招可以重新安排笔试)2026.2.4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;小鹏汇天&nbsp;技术一面,第二周收到结果&nbsp;-&gt;挂2026.2.12&nbsp;&nbsp;&nbsp;大众Cariad代招&nbsp;技术二面&nbsp;-&gt;Offer2026.2.28&nbsp;&nbsp;&nbsp;多益网络技术面试,由于风评太差,一直在犹豫要不要接面试&nbsp;-&gt;推迟-----------分割线-----------2026.3&nbsp;月前的某一天,临时去电网报名了二批计算机岗位的笔试2026.3.6&nbsp;从上家公司实习离职,氛围最好的一家公司,leader&nbsp;说可以帮忙转正,但是流程太长,而且我们部门据说只有一个&nbsp;hc,更想要研究生,我很有可能是会被签外包公司在这里干活,就离职了。2026.3.9&nbsp;入职新公司,大众Cariad&nbsp;以外部公司的身份进组,项目组签了三年,后续三年应该都可以在这里呆,不知道有没有希望原地跳槽。2026.3.10&nbsp;电网考试居然说我通过资格审查了,短信约我去参加资格审查,请假一天,买了&nbsp;12&nbsp;号晚上的机票回成都2026.3.15&nbsp;参加国家电网三新计算机类的笔试2026.3.17&nbsp;电网出成绩了,感觉很低。觉得已经🈚️了2026.3.18&nbsp;收到电网面试通知,通知&nbsp;3.22-3.25&nbsp;这个时间去面试,我的岗位只招&nbsp;1&nbsp;个人。据说面试只有&nbsp;2-3&nbsp;人,不知道能不能成功
点赞 评论 收藏
分享
评论
6
15
分享

创作者周榜

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