8.高精度减法

1.题目

给定两个正整数,计算它们的差,计算结果可能为负数。

输入格式
共两行,每行包含一个整数。

输出格式
共一行,包含所求的差。

数据范围

1≤整数长度≤105

输入样例:

32

11

输出样例:

21

2.解题

对于两个大数的减法,类似于加法,将两个数倒序放入数组中进行计算

这里需要考虑借位,所以分两种情况

(1)如果Xi - Yi - t >= 0, 则直接进行减法运算即可(Xi - Yi - t)

(2)如果Xi - Yi - t < 0, 则需要向上一位借一位再相减(Xi - Yi + 10 - t)

另外,还需要考虑相减运算后的结果是否为负,即需要判断x和y两个数哪个大

(1)如果x和y长度不一样,则哪个数长久哪个数大

(2)如果x和y长度一样,则从高位开始逐位判断每一位是否不等,如果遇到不等的位,则哪一位大就哪个数大,否则两个数相等。

对于t

如果t >= 0, t = t;
如果t < 0, t = 10

合在一起:(t + 10) % 10

方法一:

根据解题思路完成

java:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static boolean cmp(String x, String y) {
        if (x.length() != y.length())
            return x.length() > y.length();
        for (int i = 0; i < x.length(); i++) {
            if (x.charAt(i) != y.charAt(i))
                return x.charAt(i) > y.charAt(i);
        }
        return true;
    }

    public static List<Integer> sub(String x, String y) {
        int[] X = new int[x.length()];
        int[] Y = new int[y.length()];
        List<Integer> res = new ArrayList<>();

        for (int i = x.length() - 1, j = 0; i >= 0; i--, j++) {
            X[j] = x.charAt(i) - '0';
        }

        for (int i = y.length() - 1, j = 0; i >= 0; i--, j++) {
            Y[j] = y.charAt(i) - '0';
        }

        // int t = 0;// 借位
        for (int i = 0, t = 0; i < X.length; i++) {
            if (i < Y.length) {
                if (X[i] - Y[i] - t < 0) {
                    res.add(X[i] + 10 - Y[i] - t);
                    t = 1;
                } else {
                    res.add(X[i] - Y[i] - t);
                    t = 0;
                }
            } else {
                if (X[i] - t < 0) {
                    res.add(X[i] + 10 - t);
                    t = 1;
                } else {
                    res.add(X[i] - t);
                    t = 0;
                }
            }
        }

        // 去掉前导0
        int j = res.size() - 1;
        while (res.size() > 1 && res.get(j) == 0) {
            res.remove(j);
            j--;
        }

        return res;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String x = in.readLine();
        String y = in.readLine();

        boolean F = cmp(x, y);

        List<Integer> res = new ArrayList<>();

        if (!F) {
            res = sub(y, x);
            System.out.print("-");
        } else
            res = sub(x, y);
        for (int k = res.size() - 1; k >= 0; k--)
            System.out.print(res.get(k));

        in.close();
    }
}

方法二:精简版

java:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
    // 判断x和y哪个数大
    public static boolean cmp(String x, String y) {
        if (x.length() != y.length())
            return x.length() > y.length();
        for (int i = 0; i < x.length(); i++) {
            if (x.charAt(i) != y.charAt(i))
                return x.charAt(i) > y.charAt(i);
        }
        return true;
    }

    public static List<Integer> sub(String x, String y) {
        int[] X = new int[x.length()];
        int[] Y = new int[y.length()];
        List<Integer> res = new ArrayList<>();

        for (int i = x.length() - 1, j = 0; i >= 0; i--, j++) {
            X[j] = x.charAt(i) - '0';
        }

        for (int i = y.length() - 1, j = 0; i >= 0; i--, j++) {
            Y[j] = y.charAt(i) - '0';
        }

        int t = 0;// 借位
        for (int i = 0; i < X.length; i++) {
            t = X[i] - t;
            if (i < Y.length)
                t -= Y[i];
            res.add((t + 10) % 10);
            t = t < 0 ? 1 : 0;
        }

        // 去掉前导0
        int j = res.size() - 1;
        while (res.size() > 1 && res.get(j) == 0) {
            res.remove(j);
            j--;
        }

        return res;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String x = in.readLine();
        String y = in.readLine();

        boolean F = cmp(x, y);

        List<Integer> res = new ArrayList<>();

        if (!F) {
            res = sub(y, x);
            System.out.print("-");
        } else
            res = sub(x, y);
        for (int k = res.size() - 1; k >= 0; k--)
            System.out.print(res.get(k));

        in.close();
    }
}

c++:

#include <iostream>
#include <vector>

using namespace std;

// 判断x和y哪个数大
bool cmp(string x, string y)
{
    if (x.length() != y.length())
        return x.length() > y.length();
    for (int i = 0; i < x.length(); i++)
    {
        if (x[i] != y[i])
            return x[i] > y[i];
    }
    return true;
}

vector<int> sub(string x, string y)
{
    vector<int> X, Y, res;
    for (int i = x.size() - 1; i >= 0; i--)
        X.push_back(x[i] - '0');
    for (int i = y.size() - 1; i >= 0; i--)
        Y.push_back(y[i] - '0');

    int t = 0; //借位
    for (int i = 0; i < X.size(); i++)
    {
        t = X[i] - t;
        if (i < Y.size())
            t -= Y[i];
        res.push_back((t + 10) % 10);
        t = t < 0 ? 1 : 0;
    }

    while (res.size() > 1 && res.back() == 0)
        res.pop_back();

    return res;
}

int main()
{
    string x, y;
    vector<int> res;
    cin >> x >> y;

    if (cmp(x, y))
        res = sub(x, y);
    else
    {
        res = sub(y, x);
        cout << "-";
    }

    for (int j = res.size() - 1; j >= 0; j--)
        cout << res[j];
    cout << endl;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值