大数字相乘


/**
 * 大数字相乘
 */
public class Test1 {
    /**
     * ints * num
     * @param ints
     * @param num
     * @return
     */
    public static int[] mul(int[] ints,int num){
        for (int i = 0; i < ints.length; i++) {
            ints[i] *= num;
        }
        for (int i = ints.length-1; i > 0; i--) {
            ints[i-1] += ints[i]/10;
            ints[i] = ints[i]%10;
        }
        return ints;
    }

    private static void console(int[] ints){
        boolean flag = true;
        for (int i = 0; i < ints.length; i++) {
            if (flag){
                if (ints[i]!=0){
                    System.out.print(ints[i]);
                    flag = false;
                }
            }else {
                System.out.print(ints[i]);
            }
        }
        System.out.println();
    }
    /**
     * num的阶乘
     * @param num
     */
    public static int[] factorial(int num){
        int[] ints = new int[200];
        ints[ints.length-1] = 1;
        for (int i = 1; i <= num; i++) {
            ints = mul(ints,i);
        }
        return ints;
    }
    public static void main(String[] args) {
        int[] i = {1,1,0};
        int num = 11;
        // 110 * 11
        console(mul(i, num));
       // console(factorial(50));//30414093201713378043612608166064768844377641568960512000000000000
    }
}
posted @ 2019-10-05 09:52  fly_bk  阅读(187)  评论(0编辑  收藏  举报