Java寒假作业——编程题

二、编程题(ACM模式)

3 (2022百度)小红拿到了一个字符串,她想知道有多少个"baidu"型子串?所谓"baidu“型字符串,指第1个、第4个字母是辅音,第2、3、5个字符是元音,且每个字母都不相同的字符串。例如,"taigu"、"huida"、"paobu"、"baoli"等都是"baidu"型字符串。我们定义元音字母仅有'a、'e'、'i'、'o'、'u'这五种,其余字母均为辅音字母。

输入描述

baiduxiaojiabankanjiaran

输入一个字符串

输出描述

2

输出"baidu"型子串的数量

解释:"baidu"和"duoxi"合法。其中"jiaba"和"jiara"不合法,因为a出现了2次。

package 寒假作业编程题;

import java.util.*;

public class Exercise03 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("输入一个字符串:");
        String s = input.next();
        int num = baiduNum(s);
        System.out.println("输出\"baidu\"型子串的数量:" + num);
    }

    private static int baiduNum(String s) {
        StringBuffer sb = new StringBuffer(s);
        int m = 0;
        int n = 4;//m n 卡五个区间
        int count = 0;
        for (int i = 0; i <= sb.length() - 5; i++) {
            if (ok(sb.substring(m, n + 1))) {
                count++;
            }
            m++;
            n++;
        }
        return count;
    }

    private static boolean ok(String subs) {//"baidu"型子串的数量
        HashMap<Character, Integer> map =  new HashMap<>();
        for (int i = 0; i < subs.length(); i++) {
            char c = subs.charAt(i);
            map.put(c, map.getOrDefault(c, 0) + 1);
            if ((i == 0 || i == 3) && (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
                //如果第1个、第4个字母是元音
                return false;
            }
            if ((i == 1 || i == 2 || i == 4) && (c != 'a') || c !='e' || c != 'i' || c != 'o' || c != 'u') {
                //如果第2、3、5 个字符是是辅音
                return false;
            }
        }
        if (check(map)) {
            return true;
        } else {
            return false;
        }
    }

    private static boolean check(HashMap<Character, Integer> map) {//检查map中的值(字符出现次数有没有>1)
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            int val = (int) entry.getValue();
            if (val > 1) {
                return false;
            }
        }
        return true;
    }
}

4 (2022百度)小红拿到了一段java代码,请你判断其中最多有多少层for循环嵌套。保证代码合法,且不含注释。循环语句只有for,条件语句只有if,循环语句和条件语句保证包含大括号用来控制范围。代码中只有一个类和一个主函数。

输入描述

import java.util.*;

public class Main{

public static void main(String[] args){

Scanner in = new Scanner(System.in);

int a = 0 , b = 0;

int n = in.nextInt();

for (int i = 0; i < n; i++) {

if ( a < b ) {

a += b / 2;

}

for (int j = 0; j < n; j++) {

b++;

a += b;

}

for (int j = 1; j < n; j *= 2) {

b--;

}

}

System.out.println(a);

}

}

注意是多行输入

输出描述

2

输出最大嵌套数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值