Java大数相加相乘

  • 相加思想:利用对位相加,多余的对位上数字与进位相加,每一位的数字存储到字符串builder中,最后倒排吐出

代码:

 /**
     * 相加
     *
     * @param s1
     * @param s2
     * @return
     */
    public String add(String s1, String s2) {
        if (s1 == null || s1.length() == 0) {
            return s2;
        }
        if (s2 == null || s2.length() == 0)
            return s1;
        //都有值
        int idx1 = s1.length() - 1;
        int idx2 = s2.length() - 1;
        StringBuilder sb = new StringBuilder();
        boolean carry = false;
        while (idx1 >= 0 && idx2 >= 0) {
            int sum = (s1.charAt(idx1) - '0') + (s2.charAt(idx2) - '0') + (carry ? 1 : 0);
            carry = false;
            if (sum / 10 > 0) {
                carry = true;
            }
            sb.append((char) ((sum % 10) + '0'));
            idx1--;
            idx2--;
        }
        //剩余的
        while (carry || idx1 >= 0 || idx2 >= 0) {
            if (idx1 >= 0) {
                int sum = (s1.charAt(idx1) - '0') + (carry ? 1 : 0);
                if ((char) ((sum % 10) + '0') > 0)
                    carry = true;
                sb.append((char) ((sum % 10) + '0'));
                idx1--;
            } else if (idx2 >= 0) {
                int sum = (s1.charAt(idx2) - '0') + (carry ? 1 : 0);
                if ((char) ((sum % 10) + '0') > 0)
                    carry = true;
                sb.append((char) ((sum % 10) + '0'));
                idx2--;
            } else {
                sb.append('1');
                carry = false;
            }
        }
        return sb.reverse().toString();
    }
  • 相乘思想:乘法分配律,如AB*CD=AC(AD+BC)BD,将两个数组逐位相乘的结果对位存放在新的数组里,再对新数组进行进位判定,进位结束后将新数组转化成字符串输出。

代码

public String multiply(String strone, String strtwo) {
        if (strone == null || strone.length() == 0 || strtwo == null || strtwo.length() == 0) {
            return "0";
        }
        //字符串转数组存储
        int[] numone = new int[strone.length()];
        for (int i = 0; i < strone.length(); i++) {
            numone[i] = strone.charAt(i) - '0';
        }
        int[] numtwo = new int[strtwo.length()];
        for (int j = 0; j < strtwo.length(); j++) {
            numtwo[j] = strtwo.charAt(j) - '0';
        }
        //大数相乘,存储到对应位置
        int[] result = new int[strone.length() + strtwo.length()]; //最大容量是两字符串长度之和
        for (int a = 0; a < numone.length; a++) {
            for (int b = 0; b < numtwo.length; b++) {
                result[a + b] += numone[a] * numtwo[b];    //相乘,放到对应位置,并与原位置的数据叠加
            }
        }
        //进位处理,从尾部倒排计算
        for (int idx = result.length - 1; idx > 0; idx--) {
            if (result[idx] / 10 > 0) {
                result[idx - 1] += result[idx] / 10;
                result[idx] = result[idx] % 10;
            }
        }
        //数据组成字符串
        String resultStr = "";
        for (int m = 0; m < result.length - 1; m++) {
            resultStr += result[m];
        }
        return resultStr;
    }

测试:

 public static void main(String[] args) {
        System.out.println(add("91277777777777777777777777773", "8788888888888888888888888888888"));
        long s1 = System.currentTimeMillis();
        String a = "777777777777777777775555555555555555555555555555777777777777777";
        String b = "999999999999999999999999666666666666666666666666666666666666699999999999";
        System.out.println(new BigDecimal(a).multiply(new BigDecimal(b)).toString());
        System.out.println(System.currentTimeMillis() - s1);
        long s2 = System.currentTimeMillis();
        System.out.println(multiply(a, b));
        System.out.println(System.currentTimeMillis() - s2);
    }

参考:Java实现大数乘法运算 - 六耳石猴 - 博客园

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值