拼多多0312笔试

Q1-100%

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        char[] cs = scan.next().toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < cs.length; i++) {
            int cnt = 0;
            while (Character.isDigit(cs[i])) cnt = cnt * 10 + (cs[i++] - '0');
            for (int j = 0; j < cnt; j++) sb.append(cs[i]);
        }
        System.out.println(sb);
    }
}

Q2-100%

import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int t = scan.nextInt(), n = scan.nextInt();
        int[][] array = new int[t][n];
        for (int i = 0; i < t; i++) {
            int cnt1 = 0, ans = 0;
            for (int j = 0; j < n; j++) {
                array[i][j] = scan.nextInt();
                if (array[i][j] == 1) cnt1++;
            }
            ans = cnt1 / 2; // 发射第一种可以消除
            ans += n - ans * 2; // 剩下全部发射第二种炮弹
            System.out.println(ans);
        }
    }
}

Q3

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        char[][] notes = new char[n][];
        for (int i = 0; i < n; i++) {
            notes[i] = scan.next().toCharArray();
        }
        int[] limits = new int[3];
        int[] moneys = new int[3];
        for (int i = 0; i < 3; i++) {
            limits[i] = scan.nextInt();
            moneys[i] = scan.nextInt();
        }
        Character[][] cs = new Character[n][];
        for (int i = 0; i < cs.length; i++) {
            cs[i] = new Character[notes[i].length];
            for (int j = 0; j < notes[i].length; j++) cs[i][j] = notes[i][j];
            Arrays.sort(cs[i], (x, y) -> moneys[x - 'A'] - moneys[y - 'A']);
        }
        PriorityQueue<Character[]> pq = new PriorityQueue<>((x, y) -> x.length == y.length ?
                getPriority(moneys, x) - getPriority(moneys, y) : x.length - y.length);
        for (Character[] cc : cs) pq.offer(cc);
        boolean flag = true;
        int cnt = 0;
        long total = 0;
        while (!pq.isEmpty()) {
            Character[] cc = pq.poll();
            boolean f = false;
            for (char c : cc) {
                int idx = c - 'A';
                if (limits[idx] > 0) {
                    limits[idx]--;
                    cnt++;
                    total += moneys[idx];
                    f = true;
                    break;
                }
            }
            if (!f) flag = false;
        }
        System.out.println(flag ? "YES" : "NO");
        System.out.println(flag ? total : cnt);
    }

    public static int getPriority(int[] moneys, Character[] cs) {
        int t = 0;
        for (char c : cs) t += moneys[c - 'A'];
        return t;
    }
}

Q4-100%

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) array[i] = scan.nextInt();
        double[] avgs = new double[n];
        double sum = 0.;
        for (int i = 0; i < n; i++) {
            sum += array[i];
            avgs[i] = Math.round(sum / (i + 1));
            System.out.print(Math.round(avgs[i]) + " ");
        }
        System.out.println();
        for (int i = 0; i < n; i++) {
            addNum(array[i]);
            System.out.print(Math.round(findMedian()) + " ");
        }
    }

    // 大堆存更小的那一半数
    static PriorityQueue<Integer> maxHeap = new PriorityQueue<>((x, y) -> y - x);
    // 小堆存更大的那一半数
    static PriorityQueue<Integer> minHeap = new PriorityQueue<>(); // 默认最小堆个数大于或等于最大堆

    public static void addNum(int num) {
        if (maxHeap.si***Heap.size()) {
            maxHeap.offer(num);
            minHeap.offer(maxHeap.poll());
        } else {
            minHeap.offer(num);
            maxHeap.offer(minHeap.poll());
        }
    }

    public static double findMedian() {
        return minHeap.size() == maxHeap.size() ? (maxHeap.peek() + minHeap.peek()) / 2. : minHeap.peek();
    }
}

#PDD##拼多多笔试#
全部评论
中间两道都是贪心吧,hhh,毕业两年回来看看
1 回复 分享
发布于 2023-03-12 23:58 广东
想问一下,这是春招还是实习啊
点赞 回复 分享
发布于 2023-03-13 10:22 辽宁
我第三题也是95,不知道什么边界情况没考虑到
点赞 回复 分享
发布于 2023-03-13 09:04 浙江
666
点赞 回复 分享
发布于 2023-03-12 23:29 江西

相关推荐

今天周一休息,突发奇想写一篇阶段总结。如题,我已经去了一个和Java彻底毫无关联的行业。曾经我以为自己能在计算机行业发光发热,没想到刚入行一年多就当了逃兵。从最开始的热爱到现在一看到代码就厌恶,不知道自己经历了什么。所以我去干什么了?答案是:在成都当了租房销售。上班那会压力大了就念叨着去干租房中介,但是一直下不去这个决心,想着自己学了四年多的计算机知识,终究还是不甘心。终于在某一天准备八股文的时候,看着无数篇和工作内容关系不大的理论知识,那一刻下定决心,决定尝试一下销售行业,也算是给自己一个交代。后面阴差阳错的投了成都自如去当租房管家,没想到面试很顺利,在当天一百多个面试的人里面,我成为了为数不多通过的几个幸运儿之一。目前已经培训通过,正式入职,也开了单,也有压力但是每天过得很开心,真心喜欢那种和人交流的感觉,哪怕是最后没有选择找我租房。说这些也是想告诉那些大三,大四正在找Java实习而焦虑的同学:你们现在还年轻,选择很多,容错率也很高,可以尽情去尝试自己喜欢的行业和工作。不用因为某一次的面试没通过或者简历石沉大海而焦虑,更不用因为身边人都在挤编程的独木桥就强迫自己跟风。也算是自己的碎碎念吧,也希望自己能在新的领域取得一点小成就。也祝牛油工作顺利!
沉淀小子:干啥都不丢人啊,生存是必须要的,销售很考验一个人综合素质能力的,好的销售人脉和资源可不比写字楼的白领差啊
点赞 评论 收藏
分享
评论
6
18
分享

创作者周榜

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