📜  使用 9 的补码将两个大数相减

📅  最后修改于: 2021-09-07 02:28:58             🧑  作者: Mango

给定长度分别为NM 的两个字符串str1str2 ,每个字符串代表一个大数,任务是使用 9 的补码从另一个中减去一个。

例子:

方法:
基本思想类似于使用 2 的补码对两个数字进行减法。

请按照以下步骤解决问题:

  • 比较两个字符串的长度并将两者中较小的存储在str2 中
  • 计算str2 的9 的恭维
  • 现在,将str2 的9 赞美添加到str1
  • 如果生成任何进位,则在str1的开头插入。
  • 如果没有产生进位,那么str1 的补码就是最终答案。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above appraoch
#include 
using namespace std;
  
// Function to return sum of two
// large numbers given as strings
string sumBig(string a, string b)
{
  
    // Compare their lengths
    if (a.length() > b.length())
        swap(a, b);
  
    // Stores the result
    string str = "";
  
    // Store the respective lengths
    int n1 = a.length(),
        n2 = b.length();
  
    int diff = n2 - n1;
  
    // Initialize carry
    int carry = 0;
  
    // Traverse from end of both strings
    for (int i = n1 - 1; i >= 0; i--) {
        // Compute sum of
        // current digits and carry
        int sum = ((a[i] - '0')
                   + (b[i + diff] - '0')
                   + carry);
  
        // Store the result
        str.push_back(sum % 10 + '0');
  
        // Update carry
        carry = sum / 10;
    }
  
    // Add remaining digits of str2[]
    for (int i = n2 - n1 - 1; i >= 0; i--) {
  
        int sum = ((b[i] - '0') + carry);
  
        str.push_back(sum % 10 + '0');
        carry = sum / 10;
    }
  
    // Add remaining carry
    if (carry)
        str.push_back(carry + '0');
  
    // Reverse resultant string
    reverse(str.begin(), str.end());
  
    return str;
}
  
// Function return 9's
// complement of given number
string compliment9(string v)
{
    // Stores the compliment
    string compliment = "";
  
    for (int i = 0; i < v.size(); i++) {
  
        // Subtract every bit from 9
        compliment += '9' - v[i] + '0';
    }
  
    // Return the result
    return compliment;
}
  
// Function returns subtraction
// of two given numbers as strings
string subtract(string a, string b)
{
  
    // If second string is larger
    if (a.length() < b.length())
        swap(a, b);
  
    // Calculate respective lengths
    int l1 = a.length(),
        l2 = b.length();
  
    // If lengths are equal
    int diffLen = l1 - l2;
  
    for (int i = 0; i < diffLen; i++) {
  
        // Insert 0's to the beginning
        // of b to make both the lengths equal
        b = "0" + b;
    }
  
    // Add (complement of B) and A
    string c = sumBig(a, compliment9(b));
  
    // If length of new string is greater
    // than length of first string,
    // than carry is generated
    if (c.length() > a.length()) {
        string::iterator it;
  
        // bit1 is the carry bit
        char bit1 = c[0];
        string bit = { bit1 };
        it = c.begin();
        c.erase(it);
        c = sumBig(c, bit);
        return c;
    }
  
    // If both lengths are equal
    else {
        return compliment9(c);
    }
}
  
// Driver Code
int main()
{
  
    string str1 = "12345678987654321";
    string str2 = "22324324343";
  
    cout << subtract(str1, str2) << endl;
  
    return 0;
}


输出:
12345656663329978

时间复杂度: O(max(N, M))
辅助空间: O(max(N, M))