美团8.8笔试

网易的在15点,美团的在16点,想着把网易的快快答完去做美团的,结果网易的题也太难了吧。。就AC了一道,没办法了,网易笔试16:40结束,但是估计再来40分钟也做不出来,果断关了去做美团了。
美团的题还好,都有思路,大概写一写。

1 算平均评分,保留一位小数

很简单,不说了

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long val = 0;
        int count = 0;
        for (int i = 0; i < 5; i++) {
            int ac = scanner.nextInt();
            count +=ac;
            val += ac *(i+1);
        }
        double v = ((double) val) / count;
        System.out.println(Math.floor(v*10)/10);
    }
}

2 优惠券

看懂了题目意思 也没什么难度,加就行了

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int va1 = 0;
        int va2= 0;
        for (int i = 0; i < n; i++) {
            int v1 = scanner.nextInt();
            int v2 = scanner.nextInt();
            if(v1 > v2) {
                va1 += v1;
                va2 += (v1-v2);
            }else {
                va1+= v2;
            }
        }
        System.out.println(va1 + " " + va2);
    }
}

3 送花

题目给的例子太简单了,自己做的时候想错了,最后时间来不及改了,估计暴力搜索会超时就没尝试暴力,最后没AC,遗憾。
重新整理思路,补了一下代码,没办法测试了,如有问题欢迎指正。

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        Tree[] trees = new Tree[n];
        trees[0] = new Tree(1);
        for (int i = 1; i < n; i++) {
            trees[i] = new Tree(i+1);
        }
        for (int i = 1; i < n; i++) {
            int from = scanner.nextInt()-1;
            int to = scanner.nextInt()-1;
            int path = scanner.nextInt();
            trees[from].nexts.add(trees[to]);
            trees[to].father = trees[from];
            trees[to].fromHome = trees[from].fromHome + path;
            trees[to].fromFather = path;
        }
        int retVal1 = trees[0].getLengthFromThisToEveryNode();// 从花店到每个地址的距离总和
        int val = 0;
        int retVal2= 0;// 配送员一共走的路程总和
        int maxTreeFromHome = 0;
        Tree farTree = null;
        for (Tree tree: trees){
            retVal2 += tree.fromFather*2;
            if(tree.fromHome >= maxTreeFromHome){
                maxTreeFromHome = tree.fromHome;
                farTree = tree;
            }
        }
        while(farTree != null){
            val += farTree.fromFather;
            farTree = farTree.father;
        }
        retVal2-=val;
        System.out.println(retVal1+" "+retVal2);


    }
    private static class Tree{
        ArrayList<Tree> nexts;
        Tree father;
        int id;
        int fromHome;//从家到这个地址的距离
        int fromFather;//从上个地址到这个地址的距离

        public int getLengthFromThisToEveryNode(){
            int sum = this.fromHome;
            for (Tree next :nexts)
                sum += next.getLengthFromThisToEveryNode();
            return sum;
        }

        public Tree(int id) {
            this.nexts = new ArrayList<>();
            this.id = id;
        }

    }
}

4 优惠券连连看,合并优惠券得奖励金

本来想着用一个数组去存这个优惠券金额,合并掉的就标记为-1,但是不知道为什么不行,只好用ArrayList,因为优惠券金额只有1到100,所以循环100次把对应金额的合并一下就行,不能合并的留到下一轮。
本题虽然通过了测试用例,但是评论有老哥指出问题,我只考虑从左侧合并,如果出现 1 1 1 这种,合并成 1 22 1 是不一样的,我后来又想了一下,暂时没想出来,后面再更吧。

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(scanner.nextInt());
        }
        int count = 0;
        for (int i = 1; i <= 100; i++) {
            ArrayList<Integer> nlist = new ArrayList<>();
            for (int j = 0; j < list.size(); j++) {
                if(j== list.size()-1) {
                    nlist.add(list.get(j));
                    continue;
                }
                if(list.get(j) == i &&list.get(j).equals(list.get(j+1))) {
                    nlist.add(i + 1);
                    count ++;
                    j++;
                }else
                    nlist.add(list.get(j));
            }
            list = nlist;
        }
        System.out.println(count);
    }
}

5 黄黑树中不同颜色节点的深度

没有特别大的算法难度,但是不知道为什么我代码只过了91%,按理来说也没有很复杂的循环或递归,极端输入也不会出问题啊。诶,好迷

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        Tree[] trees= new Tree[n+1];
        for (int i = 1; i <= n; i++) {
            trees[i] = new Tree(i,scanner.nextInt() != 1);
        }
        for (int i = 1; i <= n-1; i++) {
            trees[scanner.nextInt()].sons.add(trees[i+1]);
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= n; i++) {
            sb.append(trees[i].calcDepth(0,trees[i].yellow)).append(" ");
        }
        System.out.println(sb.toString().trim());
    }

    private static class Tree{
        ArrayList<Tree> sons;

        int id;
        boolean yellow;

        public Tree(int id, boolean yellow) {
            this.id = id;
            this.yellow = yellow;
            this.sons = new ArrayList<>();
        }
        public int calcDepth(int already,boolean initialColor){
            int myDepth;
            if(this.yellow ==initialColor){
                myDepth = 0;
            }else
                myDepth = already;
            for (Tree son : sons){
                myDepth += son.calcDepth(already+1,initialColor);
            }
            return myDepth;
        }
    }
}

希望给个面试吧~

#笔试题目#
全部评论
LZ有相应的题目么?
点赞 回复 分享
发布于 2020-08-10 09:46
可以大致说一下题目吗
点赞 回复 分享
发布于 2020-08-10 19:48
第四题是我暑假遇到写法最难一道题 你做法根本是错的。给你个测试样例 你这样过不了的   19 6 3 3 3 3 3 12 20 5 5 5 5 5 10 2 2 4 4 8 输出:11
点赞 回复 分享
发布于 2020-08-11 09:30
投的嵌入式系统工程师,前四道一样,AC了三道,还有一道多选题。
点赞 回复 分享
发布于 2020-08-11 12:14
第三题能写一下代码码
点赞 回复 分享
发布于 2020-08-12 10:30
请问题型是只有编程题吗,看20年的还有4道问答题
点赞 回复 分享
发布于 2020-08-13 09:00
哈哈哈哈跟楼主一样 被网易难的直接关了做美团 你一面了么
点赞 回复 分享
发布于 2020-08-13 09:43

相关推荐

美丽的查理斯不讲武德:包kpi的啊,感觉虾皮一点hc都没有
点赞 评论 收藏
分享
评论
7
40
分享
牛客网
牛客企业服务