首页 > 其他分享 >大数乘法,递归计算50的阶乘

大数乘法,递归计算50的阶乘

时间:2023-03-25 14:57:01浏览次数:38  
标签:String int 50 数组 length 递归计算 ints 阶乘 static

public static void main(String[] args) {
        String result= factrial(100);
        System.out.println(result);
    }
    //计算两个数的乘积
    private static String product(String a,String b){
        //将字符串b转变为整形数字
        int intb=Integer.parseInt(b);
        int[] ints=new int[a.length()];
        //将数字字符串String转换为整形数组ints
        for (int i = 0; i < a.length(); i++) {
            ints[i]=Integer.parseInt(a.substring(i,i+1));
        }
        //将ints[i]数组*数字b将其放入ints数组中
        for (int i = 0; i < ints.length; i++) {
            ints[i]=intb*ints[i];
        }
        //进位操作 得到存为ints数组的结果
        for (int i =ints.length-1; i >0; i--) {
            ints[i-1]=ints[i-1]+ints[i]/10;
            ints[i]=ints[i]%10;
        }

        //将数组的结果拼接为字符串
        StringBuilder str=new StringBuilder("");
        for (int i = 0; i < ints.length; i++) {
              str.append(ints[i]);
        }

        return str.toString();
    }
    //递归
    private static String factrial(int n){
        if(n==1) return "1";
        else return product(factrial(n-1),Integer.toString(n));
    }

 

标签:String,int,50,数组,length,递归计算,ints,阶乘,static
From: https://www.cnblogs.com/ez-wangfan/p/17254737.html

相关文章