美团笔试真题——公司食堂

牛客网公司真题(笔试)系列

2024/3/24

美团2021校招笔试-编程题(通用编程试题,第10场)

一. 题目

在这里插入图片描述
在这里插入图片描述

二.思路

将1人桌和0人桌分别建立一个小顶堆,
遍历每个等待的人,
如果是男生,优先1人桌小顶堆;
如果是女生,优先0人桌小顶堆;

二. 自测通过,但提交超时
import java.util.Scanner;
import java.util.*;

//有一个坑是:输出桌号是1到n, 而不是0到n-1
//第二个坑是:0人桌占上一个之后,得把这个桌加入1人桌优先队列里
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int t = in.nextInt();  //有多少组数据
        for(int i = 0; i < t; i++){

            int n = in.nextInt();   //餐桌数
            int[] nums = new int[n];   //餐桌人数情况
            String str = in.next();
            for(int j = 0; j < n; j++){
                nums[j]  = str.charAt(j) - '0';
            }
            int m = in.nextInt(); //等待人数
            String str1 = in.next(); //等待男女情况
            char[] chars = new char[m];
            for(int k = 0; k < m; k++){
                chars[k]  = str1.charAt(k);
            }

            int[] result = test(n, nums, m, chars);
            for(int o = 0; o < m; o++){
                System.out.println(result[o]);
            }

        }
    }

    public static int[] test(int n, int[] nums, int m, char[] chars){
        int[] res = new int[m];

        //建两个队列是因为,2人桌已经满了,就没有可坐的位置了
        PriorityQueue<Integer> zeroTable = new PriorityQueue<>();
        PriorityQueue<Integer> oneTable = new PriorityQueue<>();

        //形成两个优先队列
        for(int i = 0; i < n; i++){
            if(nums[i] == 0){
                zeroTable.add(i);
            }else if(nums[i] == 1){
                oneTable.add(i);
            }
        }

        for(int j = 0; j < m; j++){
            
            if(chars[j] == 'M'){   //男性:优先选择1人桌
                if(oneTable.peek() != null){
                    res[j] = oneTable.poll() + 1;
                }else if(zeroTable.peek() != null){
                    oneTable.add(zeroTable.peek());
                    res[j] = zeroTable.poll() + 1;
                }
            }else{   //女性:优先选择0人桌
                if(zeroTable.peek() != null){
                    oneTable.add(zeroTable.peek());
                    res[j] = zeroTable.poll() + 1;
                }else if(oneTable.peek() != null){
                    res[j] = oneTable.poll() + 1;
                }
            }
        }

        return res;
    }
}

在这里插入图片描述
在这里插入图片描述

三. 换成输入流试试,还是不行
import java.util.*;
import java.io.*;

//有一个坑是:输出桌号是1到n, 而不是0到n-1
//第二个坑是:0人桌占上一个之后,得把这个桌加入1人桌优先队列里
public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int t = Integer.parseInt(br.readLine());  //有多少组数据
        for(int i = 0; i < t; i++){

            int n = Integer.parseInt(br.readLine());   //餐桌数
            int[] nums = new int[n];   //餐桌人数情况
            String str = br.readLine();
            for(int j = 0; j < n; j++){
                nums[j]  = str.charAt(j) - '0';
            }
            int m = Integer.parseInt(br.readLine()); //等待人数
            String str1 = br.readLine(); //等待男女情况
            char[] chars = new char[m];
            for(int k = 0; k < m; k++){
                chars[k]  = str1.charAt(k);
            }

            int[] result = test(n, nums, m, chars);
            for(int o = 0; o < m; o++){
                System.out.println(result[o]);
            }

        }
    }

    public static int[] test(int n, int[] nums, int m, char[] chars){
        int[] res = new int[m];

        //建两个队列是因为,2人桌已经满了,就没有可坐的位置了
        PriorityQueue<Integer> zeroTable = new PriorityQueue<>();
        PriorityQueue<Integer> oneTable = new PriorityQueue<>();

        //形成两个优先队列
        for(int i = 0; i < n; i++){
            if(nums[i] == 0){
                zeroTable.add(i);
            }else if(nums[i] == 1){
                oneTable.add(i);
            }
        }

        for(int j = 0; j < m; j++){
            
            if(chars[j] == 'M'){   //男性:优先选择1人桌
                if(oneTable.peek() != null){
                    res[j] = oneTable.poll() + 1;
                }else if(zeroTable.peek() != null){
                    oneTable.add(zeroTable.peek());
                    res[j] = zeroTable.poll() + 1;
                }
            }else{   //女性:优先选择0人桌
                if(zeroTable.peek() != null){
                    oneTable.add(zeroTable.peek());
                    res[j] = zeroTable.poll() + 1;
                }else if(oneTable.peek() != null){
                    res[j] = oneTable.poll() + 1;
                }
            }
        }

        return res;
    }
}

在这里插入图片描述

四. 看来得把处理数据里面多余的循环给去掉啦(此时,很多细节需要注意),同时用输出流
import java.util.*;
import java.io.*;

//有一个坑是:输出桌号是1到n, 而不是0到n-1
//第二个坑是:0人桌占上一个之后,得把这个桌加入1人桌优先队列里
public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int t = Integer.parseInt(br.readLine());  //有多少组数据
        for(int i = 0; i < t; i++){

            int n = Integer.parseInt(br.readLine());   //餐桌数
            String str = br.readLine();  //餐桌人数情况
            
            int m = Integer.parseInt(br.readLine()); //等待人数
            String str1 = br.readLine(); //等待男女情况
            
            int[] result = test(n, str, m, str1);
            for(int r : result){
                bw.write(Integer.toString(r));
                bw.newLine();
            }
             //flush()冲刷出流,将所有缓冲数据强制发送至目的地。不加这个,是无法输出的
            bw.flush();

        }
    }

    public static int[] test(int n, String str, int m, String str1){
        int[] res = new int[m];

        //建两个队列是因为,2人桌已经满了,就没有可坐的位置了
        PriorityQueue<Integer> zeroTable = new PriorityQueue<>();
        PriorityQueue<Integer> oneTable = new PriorityQueue<>();

        //形成两个优先队列
        for(int i = 0; i < n; i++){
            if(str.charAt(i) == '0'){
                zeroTable.add(i);
            }else if(str.charAt(i) == '1'){
                oneTable.add(i);
            }
        }

        for(int j = 0; j < m; j++){
            
            if(str1.charAt(j) == 'M'){   //男性:优先选择1人桌
                if(oneTable.peek() != null){
                    res[j] = oneTable.poll() + 1;
                }else if(zeroTable.peek() != null){
                    oneTable.add(zeroTable.peek());
                    res[j] = zeroTable.poll() + 1;
                }
            }else{   //女性:优先选择0人桌
                if(zeroTable.peek() != null){
                    oneTable.add(zeroTable.peek());
                    res[j] = zeroTable.poll() + 1;
                }else if(oneTable.peek() != null){
                    res[j] = oneTable.poll() + 1;
                }
            }
        }

        return res;
    }
}

通过!

在这里插入图片描述

总结:

acm模式需记得:

注意next()和nextLine()的区别:
  1. next()
    一定要读取到有效字符后才可以结束输入。
    对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
    只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
    next() 不能得到带有空格的字符串。

  2. nextLine()
    以Enter为结束符, nextLine() 方法返回的是输入回车之前的所有字符。
    可以获得空白。

坑:

使用 nextInt() 在接收完数字之后,
接收输入的位置是停留在数字后的一位,
再使用 nextLine() 接收输入时,就会接收到换行符。

  • 16
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值