美团2024 10-12笔试

第一题:求出好数的个数

package meituan;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

    static int maxPowerImprove = 0; // 最大战斗力提升
    static Map<Integer, Integer> maxXMCombatPowerMap = new HashMap<>(); // 记录XM与XT的最佳配对

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m = in.nextInt(); 
        int n = in.nextInt(); 

        int[] xmCombatPower = new int[m]; 
        int[] xtCombatPower = new int[n]; 

   
        for (int i = 0; i < m; i++) {
            xmCombatPower[i] = in.nextInt();
        }

        for (int i = 0; i < n; i++) {
            xtCombatPower[i] = in.nextInt();
        }

        int[] xmUsed = new int[m]; 
        int[] xtUsed = new int[n]; 
        Arrays.fill(xmUsed, -1); 
        Arrays.fill(xtUsed, -1);

  
        maxPowerImprove(xmUsed, xtUsed, new HashMap<>(), xmCombatPower, xtCombatPower, 0);


        System.out.println(maxPowerImprove);

  
        for (int i = 0; i < xmCombatPower.length; i++) {
            if (maxXMCombatPowerMap.containsKey(i)) {
                System.out.print(maxXMCombatPowerMap.get(i) + 1 + " "); 
            } else {
                System.out.print("-1 "); // 未匹配则输出 -1
            }
        }
    }


    public static boolean isPrime(int num) {
        if (num < 2) {
            return false;
        }
        for (int i = 2; i * i <= num; i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }

    private static void maxPowerImprove(int[] xmUsed, int[] xtUsed, Map<Integer, Integer> xmCombatPowerMap,
                                        int[] xmCombatPower, int[] xtCombatPower, int powerImprove) {
 
        if (count(xmUsed) == xmCombatPower.length || count(xtUsed) == xtCombatPower.length) {
         
            if (powerImprove > maxPowerImprove) {
                maxPowerImprove = powerImprove;
                maxXMCombatPowerMap.clear();
                maxXMCombatPowerMap.putAll(xmCombatPowerMap);
            }
            return;
        }

     
        for (int i = 0; i < xmCombatPower.length; i++) {
            if (xmUsed[i] == -1) { 
                for (int j = 0; j < xtCombatPower.length; j++) {
                    if (xtUsed[j] == -1) { 
                        int tempImprove = 0;

                       
                        if (isPrime(xmCombatPower[i]) && isPrime(xtCombatPower[j])) {
                            tempImprove = (xmCombatPower[i] + xtCombatPower[j]) * 2;
                        } else if (isPrime(xmCombatPower[i]) || isPrime(xtCombatPower[j])) {
                            tempImprove = Math.max(xmCombatPower[i], xtCombatPower[j]) * 2;
                        } else {
                            tempImprove = xmCombatPower[i] + xtCombatPower[j];
                        }
                        
                        xmUsed[i] = j;
                        xtUsed[j] = i;
                        powerImprove += tempImprove;
                        xmCombatPowerMap.put(i, j);
                        
                        maxPowerImprove(xmUsed, xtUsed, xmCombatPowerMap, xmCombatPower, xtCombatPower, powerImprove);

                        // 回溯
                        xmUsed[i] = -1;
                        xtUsed[j] = -1;
                        powerImprove -= tempImprove;
                        xmCombatPowerMap.remove(i);
                    }
                }
            }
        }
    }
    
    public static int count(int[] arr) {
        int count = 0;
        for (int i : arr) {
            if (i != -1) {
                count++;
            }
        }
        return count;
    }
}

AC

第二题:字符串第K个数字

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int k = in.nextInt();
        String s = in.next();

        if (s == null || s.isEmpty()) {
            System.out.println("N");
            return;
        }

        List<BigInteger> nums = extractNums(s);
        BigInteger result = getKthNum(nums, k);

        if (result.equals(BigInteger.valueOf(-1))) {
            System.out.println("N");
        } else {
            System.out.println(result);
        }
    }

    private static List<BigInteger> extractNums(String s) {
        List<BigInteger> nums = new ArrayList<>();
        char[] chars = s.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char c : chars) {
            if (Character.isDigit(c)) {
                sb.append(c);
            } else {
                if (sb.length() > 0) {
                    nums.add(new BigInteger(sb.toString()));
                    sb = new StringBuilder();
                }
            }
        }
        if (sb.length() > 0) {
            nums.add(new BigInteger(sb.toString()));
        }
        return nums;
    }

    private static BigInteger getKthNum(List<BigInteger> nums, int k) {
        if (k > nums.size()) {
            return BigInteger.valueOf(-1);
        }
        nums.sort((o1, o2) -> o2.compareTo(o1));
        return nums.get(k - 1);
    }
}

AC

第三题: 宠物大战

package meituan;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

    static int maxPowerImprove = 0; // 最大战斗力提升
    static Map<Integer, Integer> maxXMCombatPowerMap = new HashMap<>(); // 记录XM与XT的最佳配对

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();

        int[] xmCombatPower = new int[m];
        int[] xtCombatPower = new int[n];


        for (int i = 0; i < m; i++) {
            xmCombatPower[i] = in.nextInt();
        }

        for (int i = 0; i < n; i++) {
            xtCombatPower[i] = in.nextInt();
        }

        int[] xmUsed = new int[m];
        int[] xtUsed = new int[n];
        Arrays.fill(xmUsed, -1);
        Arrays.fill(xtUsed, -1);


        maxPowerImprove(xmUsed, xtUsed, new HashMap<>(), xmCombatPower, xtCombatPower, 0);


        System.out.println(maxPowerImprove);


        for (int i = 0; i < xmCombatPower.length; i++) {
            if (maxXMCombatPowerMap.containsKey(i)) {
                System.out.print(maxXMCombatPowerMap.get(i) + 1 + " ");
            } else {
                System.out.print("-1 "); // 未匹配则输出 -1
            }
        }
    }


    public static boolean isPrime(int num) {
        if (num < 2) {
            return false;
        }
        for (int i = 2; i * i <= num; i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }

    private static void maxPowerImprove(int[] xmUsed, int[] xtUsed, Map<Integer, Integer> xmCombatPowerMap,
                                        int[] xmCombatPower, int[] xtCombatPower, int powerImprove) {

        if (count(xmUsed) == xmCombatPower.length || count(xtUsed) == xtCombatPower.length) {

            if (powerImprove > maxPowerImprove) {
                maxPowerImprove = powerImprove;
                maxXMCombatPowerMap.clear();
                maxXMCombatPowerMap.putAll(xmCombatPowerMap);
            }
            return;
        }


        for (int i = 0; i < xmCombatPower.length; i++) {
            if (xmUsed[i] == -1) {
                for (int j = 0; j < xtCombatPower.length; j++) {
                    if (xtUsed[j] == -1) {
                        int tempImprove = 0;


                        if (isPrime(xmCombatPower[i]) && isPrime(xtCombatPower[j])) {
                            tempImprove = (xmCombatPower[i] + xtCombatPower[j]) * 2;
                        } else if (isPrime(xmCombatPower[i]) || isPrime(xtCombatPower[j])) {
                            tempImprove = Math.max(xmCombatPower[i], xtCombatPower[j]) * 2;
                        } else {
                            tempImprove = xmCombatPower[i] + xtCombatPower[j];
                        }

                        xmUsed[i] = j;
                        xtUsed[j] = i;
                        powerImprove += tempImprove;
                        xmCombatPowerMap.put(i, j);

                        maxPowerImprove(xmUsed, xtUsed, xmCombatPowerMap, xmCombatPower, xtCombatPower, powerImprove);

                        // 回溯
                        xmUsed[i] = -1;
                        xtUsed[j] = -1;
                        powerImprove -= tempImprove;
                        xmCombatPowerMap.remove(i);
                    }
                }
            }
        }
    }

    public static int count(int[] arr) {
        int count = 0;
        for (int i : arr) {
            if (i != -1) {
                count++;
            }
        }
        return count;
    }
}

好气啊!!!结束才发现,都不是质数的情况是不需要×2的,检查了半天,脑袋麻了

#美团笔试#
全部评论
你好,第一题代码好像放成第三题的了,可以发下第一题的吗?疑惑自己为什么没通过
1 回复 分享
发布于 10-12 22:57 浙江
最后一题暴搜会超时吗?
点赞 回复 分享
发布于 10-12 20:46 广东
第三题看着像暴力回溯,不会t吗
点赞 回复 分享
发布于 10-12 21:00 浙江
为啥我有5道题
点赞 回复 分享
发布于 10-12 21:03 北京

相关推荐

优化Webpack的构建速度是一个常见的需求,下面是一些常见的优化策略:https://www.nowcoder.com/issue/tutorial?zhuanlanId=Mg58Em&amp;uuid=fffb9e7b5576495f90d9596c40989b9f通过配置缓存:可以使用cache-loader或者hard-source-webpack-plugin来启用缓存,避免重复编译没有改动的文件。通过配置多线程/并行构建:可以使用thread-loader或者happypack来在多个工作线程中并行处理任务,加快构建速度。减少文件的解析和处理:可以通过配置resolve.extensions来减少Webpack的文件解析,只处理特定格式的文件。另外,使用include和exclude选项来限制需要处理的文件范围。优化Loader的配置:可以使用exclude选项来排除不必要的目录,只对需要处理的目录使用对应的Loader。另外,可以使用resolve.alias来配置别名,减少模块查找时间。使用Tree&nbsp;Shaking:通过配置mode为production,并且在package.json中将sideEffects设置为false或者具体的文件列表,开启Tree&nbsp;Shaking功能,剔除掉未使用的代码。合理使用Webpack的插件:根据具体需求,合理选择和配置Webpack的插件,避免不必要的处理和压缩。使用DllPlugin和缓存:可以将一些不经常变动的库使用DllPlugin预先编译,并将结果文件缓存起来,这样可以避免每次构建都重新编译这些库。以上是一些常见的Webpack构建速度优化策略,根据具体的项目需求和情况选择合适的优化方式。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
2 4 评论
分享
牛客网
牛客企业服务