大整数加减乘除模板

关于整数加减乘除的题很多,例如leetcode中的 29. Divide Two Integers

题意

Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

题解:

public class Solution {
    public int divide(int dividend, int divisor) {
        long dd = (dividend >= 0) ? dividend : -(long)dividend;
        long dv = (divisor >= 0) ? divisor : -(long)divisor;
        long ans = 0;
        int bit = 31;
        while (bit >= 0) {
            if(dd >= dv << bit) {
                dd -= dv << bit;
                ans += (1L<<bit);
            }
            bit--;
        }
        if((dividend^divisor)>>31 != 0) ans = - ans;
        if(ans >= Integer.MAX_VALUE) return Integer.MAX_VALUE;
        return (int)ans;
    }
}

如果题目允许,可以使用java的BigInteger,可以很方便的计算。

下面给出大整数加减乘除代码:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by jdan on 2016/10/16.
 */
public class BigInt {
    public List<Integer> add(int[] A, int[] B) {
        int aLen = A.length,
            bLen = B.length;
        if (aLen < bLen) return add(B, A);
        for (int i = 0; i < bLen; i++)
            A[i] += B[i];
        List<Integer> ans = new ArrayList<>();
        for (int i = 0; i < aLen; i++) {
            ans.add(A[i]%10);
            if (A[i] >= 10) {
                if (i == aLen - 1) ans.add(A[i]/10);
                else A[i+1]++;
            }
        }
        return ans;
    }
    //assume digit A is bigger than B
    public List<Integer> subtract(int[] A, int[] B) {
        int aLen = A.length,
            bLen = B.length;
        for (int i = 0; i < bLen; i++) {
            if (A[i] >= B[i]) A[i] -= B[i];
            else {
                A[i] = 10 + A[i] - B[i];
                A[i+1]--;
            }
        }
//        System.out.println(Arrays.toString(A));
        List<Integer> ans = new ArrayList<>();
        for (int i = 0; i < aLen; i++) {
            if (A[i] < 0) {
                A[i] += 10;
                A[i+1]--;
            }
            ans.add(A[i]);
        }
        int len = 1;
        for (int i = aLen - 1; i >= 0; i--) {
            if (A[i] != 0) {
                len = i + 1;
                break;
            }
        }
        return ans.subList(0, len);
    }
    public List<Integer> multiply(int[] A, int[] B) {
        int aLen = A.length,
            bLen = B.length;
        int[] C = new int[aLen+bLen];
        for (int i = 0; i < C.length; i++) C[i] = 0;
        for (int i = 0; i < aLen; i++) {
            for (int j = 0; j < bLen; j++) {
                C[i+j] += A[i] * B[j];
            }
        }
        for (int i = 0; i < C.length; i++) {
            if (C[i] >= 10) {
                C[i+1] += C[i] / 10;
                C[i] %= 10;
            }
        }
        int len = 1;
        for (int i = C.length - 1; i >= 0; i--) {
            if (C[i] != 0) {
                len = i + 1;
                break;
            }
        }
        List<Integer> ans = new ArrayList<>();
        for (int i = 0; i < len; i++) ans.add(C[i]);
        return ans;
    }
    //start A from begin, start B from end;
    public int subtract2(int[] A, int aLen, int[] B, int bLen) {
        if (aLen < bLen) return -1;
        int bias = B.length - bLen;
        if (aLen == bLen) {
            for (int i = aLen - 1; i >= 0; i--) {
                if (A[i] > B[i+bias]) break;
                if (A[i] < B[i+bias]) return -1;
            }
        }
        for (int i = 0; i < bLen; i++) {
            if (A[i] >= B[i+bias]) A[i] -= B[i+bias];
            else {
                A[i+1]--;
                A[i] = A[i] + 10 - B[i+bias];
            }
        }
        for (int i = aLen - 1; i >= 0; i--) {
            if (A[i] > 0) return i + 1;
        }
        return 0;
    }
    // compare the value of A and B
    public int compare(int[] A, int[] B) {
        if (A.length < B.length) return -1;
        if (A.length > B.length) return 1;
        if (A.length == B.length) {
            for (int i = A.length - 1; i >= 0; i--) {
                if (A[i] > B[i]) return 1;
                if (A[i] < B[i]) return -1;
            }
        }
        return 0;
    }
    public List<Integer> divide(int[] A, int[] B) {
        if (A.length == 0 || B.length == 0) return null;
        if (B.length == 1 && B[0] == 0) return null;
        List<Integer> ansList = new ArrayList<>();
        int t = compare(A, B);
        if (t <= 0) {
            ansList.add(t == 0 ? 1 : 0);
            return ansList;
        }
        int aLen = A.length,
            bLen = B.length;
        int nb = B.length;
        int[] ans = new int[aLen];
        for (int i = 0; i < aLen; i++) ans[i] = 0;
        int[] B2 = new int[aLen];
        int diff = aLen - bLen;
        for (int i = 0; i < diff; i++) B2[i] = 0;
        for (int i = diff; i < aLen; i++) {
            B2[i] = B[i-diff];
        }
        bLen = B2.length;
        int tp = aLen;
        while (true) {
            if (bLen < nb || aLen < nb) break;
            aLen = subtract2(A, aLen, B2, bLen);
            if (aLen < 0) {
                bLen--;
                aLen = tp;
                if (bLen > aLen) bLen = aLen;
                continue;
            }
            ans[bLen-nb]++;
            tp = aLen;
        }
        for (int i = 0; i < ans.length; i++) {
            if (ans[i] >= 10) {
                ans[i+1] += ans[i] / 10;
                ans[i] %= 10;
            }
        }
        int len = 1;
        for (int i = ans.length - 1; i >= 0; i--) {
            if (ans[i] != 0) {
                len = i + 1;
                break;
            }
        }
        for (int i = 0; i < len; i++)
            ansList.add(ans[i]);
        return ansList;
    }
    public static void main(String[] args) {
        BigInt bigInt = new BigInt();
        // the A and B index is from 0 to max, corresponding to the digital
        // in this example A = 4006002, B = 82;
        int[] A = {2, 0, 0, 6, 0, 0, 4};
        int[] B = {2, 8};
//        List<Integer> add = bigInt.add(A, B);
//        System.out.println("add = " + add.toString());
//        List<Integer> subtract = bigInt.subtract(A, B);
//        System.out.println("sub = " + subtract.toString());
//        List<Integer> multiply = bigInt.multiply(A, B);
//        System.out.println("multiply = " + multiply.toString());
        List<Integer> divide = bigInt.divide(A, B);
        System.out.println("divide = " + divide.toString());
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值