当前位置:网站首页>Calculate the number of 926 in the string to the power of 9260 of 926

Calculate the number of 926 in the string to the power of 9260 of 926

2022-06-11 19:37:00 zhangsan3333

package StringTest;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.Math.pow;

public class String10 {
    
    public static void main(String[] args) {
    
        double a = pow(12, 2);
        System.out.println("a = " + a);

        //926 Of 9260 How many strings are there in the power 926
        String sub = "926";
        BigDecimal bigDecimal = new BigDecimal(926);
        BigDecimal pow = bigDecimal.pow(9260);
        String string = pow.toString();
        System.out.println("string The length is  " + string.length());

        int count1 = countSub1(string, sub);
        System.out.println("count1 = " + count1);
        System.out.println();
        
        int count2 = countSub2(string, sub);
        System.out.println("count2 = " + count2);
    }

    private static int countSub1(String string, String sub) {
    
        int count = 0;
        long begin = System.currentTimeMillis();
        Matcher matcher = Pattern.compile(sub).matcher(string);
        while (matcher.find()) {
    
            count++;
        }
        long end = System.currentTimeMillis();
        System.out.println(" The elapsed time 1 = " + (double) (end - begin) / 1000);
        return count;
    }

    private static int countSub2(String string, String sub) {
    
        int olcCount = string.length();
        long begin = System.currentTimeMillis();
        int newCount = string.replace(sub, "").length();
        int count = (olcCount - newCount) / sub.length();
        long end = System.currentTimeMillis();
        System.out.println(" The elapsed time 2 = " + (double) (end - begin) / 1000);
        return count;
    }
}

 Insert picture description here

原网站

版权声明
本文为[zhangsan3333]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111927477475.html