蓝桥杯真题打卡|day07

Q1:三角回文

思路:

写两个函数判断是否为回文和三角数

代码:

public class Question1 {
    public static void main(String[] args) {
        for (int i = 20220514; ; i++) {
            if (isH(i) && isS(i)) {
                System.out.println(i);
                return;
            }
        }
    }

    public static boolean isH(int n) {
        String str = n + "";
        for (int i = 0; i <= str.length() / 2; i++) {
            if (str.charAt(i) != str.charAt(str.length() - 1 - i)) return false;
        }
        return true;
    }

    public static boolean isS(int n) {
        int cnt = 0;
        for (int i = 1; ; i++) {
            if (cnt > n) return false;
            if (cnt == n) return true;
            cnt += i;
        }
    }
}

Q2:数数

思路:

从2开始,每一个数去除这个数,一直除到,如果一共有12个就存起来,最后看一共有多少个,速度有点慢,得2-3分钟才跑完。

代码:

public class Question2 {
    public static void main(String[] args) {
        int cnt = 0;
        for (int i = 2333333; i <= 23333333; i++) {
            if (check(i)) {
                cnt++;
            }
        }
        System.out.println(cnt);  //25606
    }

    public static boolean check(int n) {
        int cnt = 0;
        for (int i = 2; i * i <= n; i++) {
            while (n % i == 0) {
                n /= i;
                cnt++;
            }
        }
        if (n > 1) cnt++;
        return cnt == 12;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值