百度笔试测试开发暑期实习4.12


第一题
首先需要找出其中出现次数最多的整数,如果出现次数最多的整数不唯一,则找出其中值最大的整数,记为M; 然后再找出其中出现次数最少的整数,如果出现次数最少的整数不唯一,则找出其中值最小的整数,记为N;最后计算M和N的差,即输出(M-N)。
思路:用HashMap
class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n=scan.nextInt();
        int[] nums=new int[n];

        for(int i=0;i<n;i++){
            nums[i]=scan.nextInt();
        }
        System.out.println(getM(nums)-getN(nums));
        scan.close();

    }

    public static int getM(int[] nums){
        HashMap<Integer,Integer> map =new HashMap<>();
        for(int i=0;i<nums.length;i++){
            if(map.containsKey(nums[i])){
                int tmp=map.get(nums[i]);
                map.put(nums[i],tmp+1);
            }else{
                map.put(nums[i],1);
            }
        }
        Collection<Integer> count=map.values();
        int maxCount=Collections.max(count);
        List<Integer> list=new ArrayList<>();

        for(Map.Entry<Integer,Integer> entry:map.entrySet()){
            if(maxCount==entry.getValue()){
                list.add(entry.getKey());
            }
        }
        int maxReal=Integer.MIN_VALUE;
        for(int i=0;i<list.size();i++){
            if(list.get(i)>maxReal){
                maxReal=list.get(i);
            }
        }
        return maxReal;
    }

    public static int getN(int[] nums){
        HashMap<Integer,Integer> map =new HashMap<>();

        for(int i=0;i<nums.length;i++){
            if(map.containsKey(nums[i])){
                int tmp=map.get(nums[i]);
                map.put(nums[i],tmp+1);
            }else{
                map.put(nums[i],1);
            }
        }

        Collection<Integer> count=map.values();

        int minCount=Collections.min(count);
        List<Integer> list=new ArrayList<>();
   
        for(Map.Entry<Integer,Integer> entry:map.entrySet()){
            if(minCount==entry.getValue()){
                list.add(entry.getKey());
            }
        }

        int minReal=Integer.MAX_VALUE;
        for(int i=0;i<list.size();i++){
            if(list.get(i)<minReal){
                minReal=list.get(i);
            }
        }
        return minReal;
    }

}
第二题
n个绳子,每根绳子最多切割一次,最终得到最多根长度相同的绳子,输出长度相同绳子的个数
思路: HashMap得到相同长度最多的绳子,
public class test1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n=scan.nextInt();
        int[] nums=new int[n];

        for(int i=0;i<n;i++){
            nums[i]=scan.nextInt();
        }

        System.out.println(lines(nums));
        scan.close();

    }

    public static int lines(int[] nums){
        HashMap<Integer,Integer> map =new HashMap<>();

        for(int i=0;i<nums.length;i++){
            if(map.containsKey(nums[i])){

                map.put(nums[i],map.get(nums[i])+1);
            }else{
                map.put(nums[i],1);
            }
        }
        int max= Collections.max(map.values());//长度相同的绳子 ,若绳子为2,3,3,5,5  则max=2        
                Set<Integer> set=new HashSet<>();

        for(Map.Entry<Integer,Integer> entry1 : map.entrySet()){
            if(entry1.getValue()==max){
                set.add(entry1.getKey());//若绳子为2,3,3,5,5  则set为{3,5}
            }
        }

        int M=Collections.min(set);//若绳子为2,3,3,5,5,则M=3
        double n=M/2.0; //n=1.5
        int sum=2*max;  //4   相当于把长为3的绳子分割了
        for(int i=0;i<nums.length;i++){
            if(M!=nums[i] && nums[i]>=M/2.0){
                sum++;
            }
        }
        return sum;
    }
}



#百度笔试##春招##实习##笔试题目##笔经##笔试时间##Java#
全部评论
一共就两题吗
点赞 回复 分享
发布于 2022-04-17 21:38
楼主你好呀,我是这周二笔试,也是测开暑期实习,可以告诉我一下题型有哪些还有难度分布嘛,感激不尽!!😊
点赞 回复 分享
发布于 2022-04-17 21:20
同4.12测开,楼主收到面试了吗
点赞 回复 分享
发布于 2022-04-17 16:47
同,请问有收到面试通知吗
点赞 回复 分享
发布于 2022-04-15 08:48
第二题通过了多少啊,我用了两层for暴力超时了orz也没ac
点赞 回复 分享
发布于 2022-04-13 10:27

相关推荐

评论
点赞
9
分享

创作者周榜

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