牛客网——美团2024届秋招笔试第三场编程真题

在这里插入图片描述

😍😍😍 相知

刷题不要一上来就直接干,先看题,明白题说的什么意思,然后想一下用什么现成的算法和数据结构可以快速解决,如果还是无从下手,建议先去看视频,不要直接翻评论或官方代码实现,看完视频,自己在idea中模拟敲几遍代码,如果跑通了,先别急着上leetcode黏贴,而是再回顾一下要点,然后确定自己完全懂了后,在leetcode中手敲,注意是手敲下来!!! 目前我就是采用的这种方法,虽然慢,但是可以维持一周忘不掉它,如果要想长期不忘,只能隔段时间就review一下了,就算是大牛,知道方法,长时间不碰,也不可能保证一次到位把代码敲完一遍过!!!

🙌🙌🙌 相识

本阶段我们要一起学习的是美团2024届秋招笔试第三场编程真题!!!

😢😢😢 开始刷题

1. 平均数为k的最长连续子数组

在这里插入图片描述

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int ans = -1;
        long pre = 0, cur=0;
        Map<Long, Integer> map = new HashMap<>();
        map.put(0L, 0);
        for (int i = 1; i <= n; i++) {
            cur = pre + sc.nextInt() - k;
            if (map.containsKey(cur)) ans = Math.max(ans, i - map.get(cur));
            else map.put(cur, i);
            pre = cur;
        }
        System.out.println(ans);
        

    }
}

2. 小球投盒

在这里插入图片描述

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        HashSet<Integer> hashset = new HashSet<>();
        for(int i = 1;i<=a;i++){
            hashset.add(i);
        }
        for(int i = 0;i < b;i++){
            int left = in.nextInt();
            int right = in.nextInt();
            if(left == 1){
                if(hashset.contains(right)){
                    hashset.remove(right);
                }
                if(hashset.size()==0){
                    System.out.print(i+1);
                    return;
                }
            }
            else{
                if(!hashset.contains(right)){
                    System.out.print(i+1);
                    return;
                }
                hashset = new HashSet<>();
                hashset.add(right);
            

            }
        }
        System.out.print(-1);
        
    }
}

3. 小红结账

在这里插入图片描述

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int m = in.nextInt();
        ArrayList<Long> arraylist = new ArrayList<>();
        for(int i = 0;i<m;i++){
            arraylist.add(0l);
        }
        for(int i  = 0;i<n;i++){
            //每张账单
            int people = in.nextInt();
            int money = in.nextInt();
            long fenmony = (long)Math.ceil(money*1.0/(people));
            for(int j = 0;j<people-1;j++){
                int who = in.nextInt();
                arraylist.set(who-1,fenmony+(long)arraylist.get(who-1));
            }
        }
        for(int i = 0;i<arraylist.size();i++){
            System.out.print(arraylist.get(i));
            if(i!=arraylist.size()-1) System.out.print(" ");
        }
    }
}

4. 小美的游戏

在这里插入图片描述

    import java.util.*;   
 
   
    // 注意类名必须为 Main, 不要有任何 package xxx 信息   
   
    public class Main {   
   
        public static void main(String[] args) {   
 
   
            long mod = 1000000007;   
            Scanner in = new Scanner(System.in);   
            int len = in.nextInt();   
            int max_op = in.nextInt();   
            long res = 0L;   
            long sum = 0L;   
            long[] a = new long[len];   
            for(int i=0;i<len;i++){   
                a[i] = in.nextInt();   
            }   
            Arrays.sort(a);   
            int idx = len-1;   
            for(int j=1;j<=max_op;j++){   
                if(a.length>=2){   
                    long mul = (a[idx] * a[idx-1])%mod;   
                    a[idx-1] = mul;   
                    a[idx] = 1;   
                    idx = idx - 1;   

                }else{   
                    res = Math.max(res, a[0]);   
                    break;   
   
                }   
            }   
   
            for(int j=0;j<len;j++){   
                sum = sum + a[j];   
            }   
            res = Math.max(res,sum%mod);   
            System.out.print(res);   
   
        }   
   
    }   

5. 小美种果树

在这里插入图片描述

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x = in.nextInt();
        int y = in.nextInt();
        int z = in.nextInt();
        int index = 0;
        //模拟???
        while(true){
            index++;
            z -=x;
            z -=y; 
            if(z<=0)break;
            index++;
            z -=x;
            if(z<=0)break;
            index++;
            z -=x;
            if(z<=0)break;
        }
        System.out.print(index);

    }
}

6. 小美的数组重排

在这里插入图片描述

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int q = in.nextInt();
        for (int i = 0; i < q; i++) {
            int n = in.nextInt();
            int m = in.nextInt();
            int [] a = new int[n];
            for (int j = 0; j < n; j++) {
                a[j] = in.nextInt();
            }
            int [] b = new int[n];
            for (int j = 0; j < n; j++) {
                b[j] = in.nextInt();
            }
            Arrays.sort(a);
            Arrays.sort(b);
            int[] reversedArray = new int[n];
            for (int j = 0; j < n; j++) {
                reversedArray[j] = b[n - 1 - j];
            }
            boolean flag = false;
            for(int j = 0;j<n;j++){
                if(a[j]+reversedArray[j]<1||a[j]+reversedArray[j]>m){
                    System.out.println("No");
                    //if(i!=n-1)System.out.println();
                    flag = true;
                    break;
                }
            }
            if(!flag){
                 System.out.println("Yes");
                 //if(i!=n-1)System.out.println();
            }
            
        }

    }
}

7. 判断ip地址是否合法

在这里插入图片描述

import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String addStr = scanner.nextLine();
        //非空
        if (addStr.equals("") || addStr == null) {
            System.out.println("invalid");
            return;
        }
        char c1 = addStr.charAt(0);
        char c2 = addStr.charAt(addStr.length() - 1);
        //4.IP地址不能以点号开始或结束,例如.192.168.0.1和192.168.0.1.是非法的。
        String s1 = Character.toString(c1);
        String s2 = Character.toString(c2);
        String[] strings = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"  };
        int temp = 0;
        for (String string : strings) {
            if (string.equals(s1)) {
                temp++;
            }
            if (string.equals(s2)) {
                temp++;
            }
        }
        if (temp != 2) {
            System.out.println("invalid");
            return;
        }
        //1.IP地址由四个数字组成,用点号分隔。
        String[] split = addStr.split("\\.");
        if (split.length != 4) {
            System.out.println("invalid");
            return;
        }
        //3.数字之间没有多余的前导零,例如01是非法的。
        for (String s : split) {
            if (s.length() == 2 && Character.toString(s.charAt(0)).equals("0")) {
                System.out.println("invalid");
                return;
            }
        }

        for (int i = 0; i < split.length; i++) {
            //2.每个数字的取值范围是0到255。
            try {
                if (Integer.parseInt(split[i]) > 255) {
                    System.out.println("invalid");
                    return;
                }
            } catch (NumberFormatException e) {
                System.out.println("invalid");
                return;
            }
        }
        int address1 = Integer.parseInt(split[0]);
        int address2 = Integer.parseInt(split[1]);
        int address3 = Integer.parseInt(split[2]);
        int address4 = Integer.parseInt(split[3]);
        if (address1 < 126 && address1 > 0 || address1 == 126 && address2 == 0 &&
                address3 == 0 && address4 == 0) {
            System.out.println("A_address");
        } else if (address1 >= 128 && address1 <= 191) {
            System.out.println("B_address");
        } else if (address1 >= 192 && address1 <= 223) {
            System.out.println("C_address");
        } else {
            System.out.println("other");
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Matthew

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值