质数串

1648545-20190708113147972-354437997.png

多少个非空连续子串是质数串!!!!!~~~~以为是各种组合 记一条审题不清晰
看了一篇博客才知道 wc https://www.cnblogs.com/bxd123/p/10658075.html
(一拿到题感觉是dp 因为之前做过这种类似的字符dp 但是想不出来状态转移)我也觉得要动归 就是不知道怎么写
题目暗示了质数串的条件非常严苛 所以可以枚举10000里面的答案
很快发现只有
2 3 5 7 23 37 53 73 373 (也就是仅有这种组合而已)

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            while (n-- > 0) {
                int lent = sc.nextInt();
                String s = sc.next() + "X" + "X";
                int count = 0;
                for (int i = 0; i < lent; i++) {
                    if (s.charAt(i) == '2')
                        count++;
                    if (s.charAt(i) == '3')
                        count++;
                    if (s.charAt(i) == '5')
                        count++;
                    if (s.charAt(i) == '7')
                        count++;
                    if (s.charAt(i) == '2' && s.charAt(i + 1) == '3')
                        count++;
                    if (s.charAt(i) == '3' && s.charAt(i + 1) == '7')
                        count++;
                    if (s.charAt(i) == '5' && s.charAt(i + 1) == '3')
                        count++;
                    if (s.charAt(i) == '7' && s.charAt(i + 1) == '3')
                        count++;
                    if (s.charAt(i) == '3' && s.charAt(i + 1) == '7' && s.charAt(i + 2) == '3')
                        count++;
                }
                System.out.println(count);
            }
        }
    }
}

转载于:https://www.cnblogs.com/cznczai/p/11150148.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值