美文网首页
大数乘法

大数乘法

作者: 敲一手烂代码 | 来源:发表于2017-03-27 17:01 被阅读19次
    static String bigNumMul(String num1,String num2) {
            char num1Chars[] = num1.toCharArray();
            char num2Chars[] = num2.toCharArray();
            int num1Ary[] = new int[num1Chars.length];
            for (int i = 0; i < num1Ary.length; i++) {
                num1Ary[i] = num1Chars[i] - '0';
            }
            
            int num2Ary[] = new int[num2Chars.length];
            for (int i = 0; i < num2Ary.length; i++) {
                num2Ary[i] = num2Chars[i] - '0';
            }
            
            int resAry[] = new int[num1Chars.length + num2Chars.length - 1];
            for (int i = 0; i < num1Ary.length; i++) {
                for (int j = 0; j < num2Ary.length; j++) {
                    resAry[i + j] += num1Ary[i] * num2Ary[j];
                }
            }
            
            for (int i = resAry.length - 1; i > 0; i--) {
                resAry[i - 1] += resAry[i] / 10;
                resAry[i] = resAry[i] % 10;
            }
            int shouwei = resAry[0] / 10;
            resAry[0] = resAry[0] % 10;
            
            String res = shouwei == 0 ? "" : "1";
            for (int i = 0; i < resAry.length; i++) {
                res += resAry[i];
            }
            return res;
        }
    

    相关文章

      网友评论

          本文标题:大数乘法

          本文链接:https://www.haomeiwen.com/subject/mjvrottx.html