百度笔试9.13

1.输入一个字符串,判断有几个”baidu“子串,每个字母不相同,第1,4个字母是元音,其他字母是辅音
直接暴力a了。。。
import java.util.HashMap;
import java.util.Scanner;

public class _baidu1 {
    public static boolean panduan(String substr){

        HashMap<Character,Integer> map1 = new HashMap<>();
        map1.put('a',1);
        map1.put('e',1);
        map1.put('i',1);
        map1.put('o',1);
        map1.put('u',1);
        if (map1.containsKey(substr.charAt(1)) && map1.containsKey(substr.charAt(2)) && map1.containsKey(substr.charAt(4))
                && substr.charAt(1)!=substr.charAt(2) && substr.charAt(1)!=substr.charAt(4) && substr.charAt(2)!=substr.charAt(4)){
            if (!map1.containsKey(substr.charAt(0)) && !map1.containsKey(substr.charAt(3)) && substr.charAt(0)!=substr.charAt(3)){
                return true;
            }
            else
                return false;
        }
        else
            return false;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int count = 0;
        for (int i=0; i<str.length()-4; i++){
            if (panduan(str.substring(i,i+5))){
                count++;
                i+=2;
            }
        }
        System.out.println(count);
    }
}

2.n个怪兽有a0,...,an血,小红有2个技能,一技能消耗1点蓝量给任意位置怪兽1点伤害,二技能消耗5点蓝量只能给队列第一只怪兽造成1点伤害,后面第1只造成2点伤害,后面第2只造成3点伤害,求最小消耗蓝量
没注意一技能放任意位置,样例过了得分0,直接混了5分
import java.util.Scanner;

public class _baidu2 {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums = new int[n];
        int sum = 0;
        for (int i=0; i<n; i++){
            nums[i] = sc.nextInt();
        }

        for (int i=0; i<n; i++){
            while (nums[i]>0 && i<n-2 && nums[i+1]>1 && nums[i+2]>1 && nums[i+1]+nums[i+2]>4){
                sum+=5;
                nums[i]-=1;
                nums[i+1] = nums[i+1]>=2 ? nums[i+1]-2:0 ;
                nums[i+2] = nums[i+2]>=3 ? nums[i+2]-3:0 ;
            }
            sum+=nums[i];
        }
        System.out.println(sum);
    }
}

3。一段代码作为n行字符串读入,判断最高有几层for循环
读了题没做。去做微众了。。感觉是提取for{},有大佬a了吗请教

#百度笔试##百度23秋招笔试编程题有点儿简单啊#
全部评论
第三题结束后自己琢磨的提供个思路,而且这个输入应该怎么处理,中间可能有多个空行     public static int maxCount=0;   int start=0;     int end=al.size();         for (int i = start; i < end; i++) {             if(al.get(i).matches(" *for.*\\(.*")){start=i;    int mark=0;                 for (int j = i; j < end; j++) { if(al.get(j).indexOf('{&(31183)#39;)>-1){mark++;}if(al.get(j).indexOf('}&(31182)#39;)>-1){ mark--; }if(mark==0){ end=j;break;} }                 dfs(al,count+1,start+1,end); i=end;  } }   System.out.println(maxCount);   }     public static void dfs(ArrayList<String> al, int count, int x,int y){ if(count>maxCount)maxCount=count;        int start=0;        int end=0;         for (int i = x; i < y; i++) {        if(al.get(i).matches(" *for.*\\(.*")){  start=i;    int mark=0;                for (int j = i; j <y; j++) {    if(al.get(j).indexOf('{&(31183)#39;)>-1){ mark++;  } if(al.get(j).indexOf('}&(31182)#39;)>-1){ mark--; }     if(mark==0){ end=j;break; }                 }  dfs(al,count+1,start+1,end);          i=end;}   } }
1 回复
分享
发布于 2022-09-14 00:43 天津
你是几卷,咋我的题目和你的不一样
点赞 回复
分享
发布于 2022-09-14 13:33 重庆
滴滴
校招火热招聘中
官网直投

相关推荐

3 4 评论
分享
牛客网
牛客企业服务