【问题标题】:Subtracting two long positive integers in an array c++ [duplicate]在数组c ++中减去两个长正整数[重复]
【发布时间】:2013-02-14 13:01:13
【问题描述】:

我对编程比较陌生 :)。

假设我想创建一个程序,提示用户输入两个最长 50 位的正数,然后从第一个数字中减去第二个数字。

例如:

用户输入第一个正数: 239834095803945862440385983452184985298358

第二个号码: 939542309853120721934217021372984729812

================================================ ==============================

程序输出差异: 238894553494092741718901766430812000568564

或,如果是否定的: -29837430045

================================================ ==============================

数字的每个数字都将作为单独的元素存储在数组中。以下是我目前接受用户输入的方式:

int read_array(int int_array[], int MAX_SIZE) {
    char number;
    int count = 0;
    //set all array entries to 0. 
    for (int i = 0; i < MAX_SIZE; i++){
        int_array[i] = 0;
    }

    do { //processes each individual char in the istream
        cin.get(number);
        // puts char on to the array until it hits the
        // end of the number (end of the line)
        if(( number != '\n') && (count < MAX_SIZE) && (isdigit(number))) {
            int_array[count] = int(number) - int('0');
        }  
        count++; //increments count
    } while (number != '\n');

    //tests if number is too large
    int digitcount = count - 1;
    if (digitcount > MAX_SIZE) {
        cout << endl << "ERROR: The number is above 50 digits!" << endl;
        return 0;
    }

问题:

如何做减法让我感到困惑。我已经尝试解决这个问题两周了,很可能是我错过了一些微不足道的事情。

我试过了:

  1. 将元素数组转换回一个完整的整数
  2. 编写自己的程序对数字进行长减法

等等……

但是,只有在达到一定数量的数字和/或它们是正数/负数之前,输出才会成功。我很困惑,我不确定最好的方法是减去两个正数数组以获得可以容纳正数和负数的成功输出,如示例中所示。非常感谢任何帮助:)。

编辑:我的尝试:

#include "long_sub.h"
#include <sstream>
#include <vector>

using namespace std;

int long_sub(int a[], int b[], const int size) {
    stringstream ss;
    int const sizes = 50;
    int c = 0; //borrow number
    int borrow = 1; // the '1' that gets carried to the borrowed number
    int r[sizes];

    for (int i = 0; i < size; i++) {
        r[i] = 0;
    }    
    //initialise answer array to 0.
    for (int i = size - 1; i >= 0; i--) {
        //handles zeros
        if (a[i] < b[i] && a[i]) {
            //takes the borrow from the next unit and appends to a.
            ss << borrow << a[i];
            ss >> c;
            ss.clear(); // clears stringstream for next potential borrow.

            int temp = c - b[i];
            r[i] = abs(temp);
        } else {
            int temp = a[i] - b[i];
            r[i] = abs(temp);
        }
    }

    for (int i = 0; i <= size - 1; i++ ) {
        cout << r[i];
    }
    cout << endl;
    return r[sizes];
}

【问题讨论】:

  • 尝试求解8-9,然后是27-46。应该让你去。
  • 你会如何用铅笔和纸做减法?那是你的算法。
  • 我认为您应该分享一些未能获得更多帮助的尝试(我的意思是代码)
  • 如果这是重复的,那么我很抱歉,我在发布之前已经彻底搜索了!
  • 我认为你的数学不应该使用“abs”。

标签: c++ arrays math subtraction


【解决方案1】:

所以解决这个问题的方法与手动操作几乎相同。

如果我们有:

 4321
-1234

您将取两个数字中的最后一位,从上面的一位中减去下面的一位,1 - 4 - 这当然意味着您必须从下一位数字中借用,所以我们记住这一点,然后来加上 7。现在,取下一个数字 [并记住“借位”],然后从 2-1 = 8 中减去 3。

完全相同的事情是您如何在计算机中对大数进行减法 - 一次做一点,如果您“借用”,那么您需要将其带到下一步。

【讨论】:

  • 感谢您的快速回复:)。事实上,我确实编写了一个程序来做到这一点。它起到了一定的作用。但是,它卡在零上并从零借用...但是,我突然想到我没有任何东西可以跟踪数字长度,我将尝试将其包括在内,看看它是否一直给我狡猾的输出:)
  • 当然,如果逐位处理长数字令人厌烦,您始终可以处理 3 位组(从最低有效位开始计算)并对其进行算术运算,不要忘记携带者。
  • 我添加了一个我的长减法代码的例子,让你在你自己的闲暇时撕开,哈哈:)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
  • 2020-08-12
  • 2022-11-17
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多