java实现大数减法算法

public static char[] subTract(int[] a,int[] b){
    int cLength = b.length;
    char sign = '+';
    // 长度大,肯定正数
    if(a.length > b.length){
        cLength = a.length;
        sign = '+';
    } else if(a.length == b.length) {//长度等于,要逐位比较
        int i = 0;
        // 找出哪一位值不同
        while(i<cLength && a[i] == b[i]){
            i++;
        }
        // 如果全都相同,直接返回值为一个0的数组
        if(i == cLength){
            return new char[]{'0'};
        }
        cLength = cLength - i;
        if(a[i] > b[i]){
            sign = '+';
        } else {
            sign = '-';
        }
    } else {// 长度小于,肯定负数
        sign = '-';
        cLength = b.length;
    }
    if(sign == '+'){
        int[] c = subWhenAbig(a,b,cLength);
        char[] result = new char[c.length];
        for(int i=0;i<c.length;i++){
            result[i] = String.valueOf(c[i]).toCharArray()[0];
        }
        return result;
    } else {
        int[] c = subWhenAbig(b,a,cLength);
        char[] result = new char[c.length + 1];
        result[0] = '-';
        for(int i=0;i<c.length;i++){
            result[i+1] = String.valueOf(c[i]).toCharArray()[0];
        }
        return result;
    }
}

private static int[] subWhenAbig(int[] a, int[] b, int cLength) {
    int[] c = new int[cLength];

    int i = a.length -1;
    int j = b.length -1;
    int k = c.length -1;
    while(i >= 0){
        if(a[i] < 0){
            a[i] = 10 + a[i];
            a[i-1] = a[i-1] - 1;
        }
        if(j < 0){
            c[k] = a[i];
        } else {
            if(a[i] >= b[j]){
                c[k] = a[i] - b[j];
            } else {
                c[k] = a[i] - b[j] + 10;
                a[i-1] = a[i-1] - 1;
            }
        }
        i--;j--;k--;
    }
    boolean firstZero = true;
    for(int x = 0;x < c.length;x++){
        if(c[x] == 0 && firstZero){
            cLength--;
        } else {
            firstZero = false;
        }
    }
    int[] result = new int[cLength];
    int y = 0;
    for(int x = c.length - cLength;x < c.length;x++,y++){
        result[y] = c[x];
    }
    return result;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java链表实现大数减法的示例代码: ```java import java.util.*; public class BigIntegerSubtraction { public static void main(String[] args) { // 生成两个随机数 String num1 = generateRandomNumber(); String num2 = generateRandomNumber(); System.out.println("num1: " + num1); System.out.println("num2: " + num2); // 将两个数存储到链表中 LinkList list1 = new LinkList(); LinkList list2 = new LinkList(); for (int i = num1.length() - 1; i >= 0; i--) { list1.Insert(num1.charAt(i) - '0'); } for (int i = num2.length() - 1; i >= 0; i--) { list2.Insert(num2.charAt(i) - '0'); } // 对两个链表进行减法操作 LinkList result = new LinkList(); int borrow = 0; Node p = list1.front, q = list2.front; while (p != null || q != null) { int x = p != null ? p.data : 0; int y = q != null ? q.data : 0; int z = x - y - borrow; if (z < 0) { z += 10; borrow = 1; } else { borrow = 0; } result.Insert(z); if (p != null) { p = p.next; } if (q != null) { q = q.next; } } // 输出结果 System.out.print("result: "); result.PrintList(); } // 生成1~50位的随机数 public static String generateRandomNumber() { Random random = new Random(); int length = random.nextInt(50) + 1; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { sb.append(random.nextInt(10)); } return sb.toString(); } } class LinkList { public Node front; public int length = 0; public LinkList() { front = null; } public void Insert(int data) { Node newNode = new Node(data); if (front == null) { front = newNode; } else { Node p = front; while (p.next != null) { p = p.next; } p.next = newNode; } length++; } public void GetList() { Node p = front; while (p != null) { System.out.print(p.data + " "); p = p.next; } System.out.println(); } public void PrintList() { Node p = front; while (p != null && p.data == 0) { p = p.next; } if (p == null) { System.out.print("0"); } else { while (p != null) { System.out.print(p.data); p = p.next; } } System.out.println(); } public int GetRear() { Node p = front; while (p.next != null) { p = p.next; } return p.data; } } class Node { public int data; public Node next; public Node(int data) { this.data = data; next = null; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值