微众银行Java笔试

20道选择,3道算法。
1.征服山顶
传入一个数组,每个位置代表山的高度。
当你爬上山,周围没有比你高的山,你的征服感就加一。
解法:
遍历两次。
第一次遍历找出最高的山。
第二次遍历,每碰到跟最高山相等的山,征服感就加一。
package webank;

import java.util.Scanner;

class Solution{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[]h = new int[n];
        for(int i = 0;i<n;i++){
            h[i] = input.nextInt();
        }
        System.out.println(calculateHappy(h));
        input.close();
    }
    public static int calculateHappy(int[]h){
        int res = 0;
        int max = 0;
        for(int i = 0;i<h.length;i++){
            if(h[i]>max){
                max = h[i];
            }
        }
        for(int i = 0;i<h.length;i++){
            if(h[i]==max){
                res++;
            }
        }

        return res;
    }


}


2.积水
你有n个蓄水池,蓄水池还有m个通道。求下雨后的蓄水池。
这题主要是输入输出耗费了比较多的时间。
而且我感觉我的解法也不好。
运用了大量的集合来做。求大神指点。
package webank;

import java.util.*;

public class Main2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s1 = input.nextLine();
        String ss1[] = s1.split(" ");
        int n = Integer.parseInt(ss1[0]);
        int m = Integer.parseInt(ss1[1]);
        float []p = new float[n];
        String s2 = input.nextLine();
        String ss2[] = s2.split(" ");
        for(int i = 0;i<n;i++){
            p[i] = Integer.parseInt(ss2[i]);
        }
        List<Set<Integer>>  list = new ArrayList<>();
        for(int i = 0;i<m;i++){
            String s3 = input.nextLine();
            String ss3[] = s3.split(" ");
            int t1 = Integer.parseInt(ss3[0])-1;
            int t2 = Integer.parseInt(ss3[1])-1;
            if(i==0){
                Set<Integer> set0= new HashSet<>();
                set0.add(t1);
                set0.add(t2);
                list.add(set0);
            }
            for(Set<Integer> set:list){
                if(set.contains(t1)||set.contains(t2)){
                    set.add(t1);
                    set.add(t2);
                }else{
                    Set<Integer> sett= new HashSet<>();
                    sett.add(t1);
                    sett.add(t2);
                    list.add(sett);
                    break;
                }
            }
        }
        for(Set<Integer> set:list){
            List<Integer>l = new ArrayList<>();
            float temp = 0;
            for(int i:set){
                l.add(i);
                temp+=p[i];
            }
            temp = temp/l.size();
            temp = Math.round(temp*100);
            temp = temp/100;
            for(int i = 0;i<l.size();i++){
                p[l.get(i)] = temp;
            }
        }
        System.out.println(p);
    }
}

3.拼积木
在第二题耗费了太多时间。。
我记得我之前在别人那边见过,不过没写,没想到又出现了。。。。


总结:1.两道题目应该能写出来,不过第二题的Math.round()的处理方法不好,导致最后结果没有通过。其实也是因为自己解法不好,耗费了大量时间。
2.其他家的真题也要好好把握,可能会出现重复。
3.有背景的题目,读懂题目,写好输入输出是关键。


#Java工程师##笔经##微众银行#
全部评论
大佬666,这个写法可以 temp = Math.round(temp*100); temp = temp/100; 之前一直想着怎么取两位小数,终究还是没取出来 double f = 3.6666666667; BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(f1);    //输出3.67
点赞 回复
分享
发布于 2021-05-13 22:08
第二题a了多少呀
点赞 回复
分享
发布于 2021-05-13 22:22
小红书
校招火热招聘中
官网直投
第二题算法不变情况下从9优化到了73 相当离谱 主要是空间问题
点赞 回复
分享
发布于 2021-05-14 07:05
第二题是并查***爆int
点赞 回复
分享
发布于 2021-05-14 08:53
第二题想半天输出两位小数
点赞 回复
分享
发布于 2021-05-14 15:57
3题分别是,90%,18%,18%。 第二题我感觉算法没问题,不知道咋一直18%
点赞 回复
分享
发布于 2021-05-14 17:02
我是4月考的,这题目。。和我当时考的一模一样啊😅
点赞 回复
分享
发布于 2021-05-19 09:03
lz收到面试通知了吗
点赞 回复
分享
发布于 2021-05-21 15:51

相关推荐

2 24 评论
分享
牛客网
牛客企业服务