减去的数字使用数组 - C ++ [英] Subtract numbers using arrays - C++

查看:113
本文介绍了减去的数字使用数组 - C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要计算两个数字(之间的差异让我们说 v N ,所以 VN <​​/ code>)使用数组(不要问我为什么这样做)。每个号码的阵列以如下方式制造:


  • 他们的能力之间的的最大的位数v N (= <$ C $数ç>问:在code)

  • VARRAY [I] = I 第v 的数字除了前导零填补了整个阵列

  • nArray [I] = - I 日的数字 N 除了前导零填补了整个阵列

例如,选择 v = 10和 N = 2,那么,

  VARRAY = [1,0]
nArray = [0,-2]

所以我写了这个code来计算阵列将等于差的数字(和= [0 9] 上面的例子):

 长R = 0;
对(INT I = Q-1;我-1个; I - ){
    总和[I] = VARRAY [I] + nArray [I]
    如果(总和[1] - ; 0){
        R =地板(总和[I] / 10);
        总和[I-1] - = R;
        总和[I] =总和[I] +10;
    }其他{
        R = 0;
    }    的NSLog(@%礼,总和[I]);
}

的问题是,总和阵列不是等于它应该是什么。对于同样的例子,和= [1,8] 什么是在code中的问题?

请注意: VARRAY nArray 是否正确生成

编辑:有几个例子和预期的结果。

  V = | N = | VARRAY = | nArray = |总和=
    25 | 9 | [2,5] | [0,9] | [1,6]
    105 | 10 | [1,0,5] | [0,1,0] | [0,9,5]
   1956年| 132 | [1,9,5,6] | [0,1,3,2] | [1,8,2,4]
  369375 | 6593 | [3,6,9,3,7,5] | [0,0,6,5,9,3] | [3,6,2,7,8,2]


解决方案

我相信我理解数据结构,如您正在使用的大整数的再presentation。

给出的数字:1234

您V阵列是:[1,2,3,4]

要添加的所有数字(又名总和),我不明白为什么要做到这一点,是:

  INT digit_sum = 0;
的for(int i = 0;我4;;我++)
{
    digit_sum + = V [I]
}

要重新presentation转换为正常,试试这个:

  int值= 0;
对(INT I = 0; I&4; ++ⅰ)
{
  值=(值* 10)+ V [I]
}

要执行一个减法,你将不得不执行的步骤,如果你的手这样做。此外,您还需要第二个号码了。

修改1:链接大数量减去结果
这可能帮助:搜索
大数量减去用C 结果
C ++大数算术

I want to calculate the difference between two numbers (let's say v and n, so v-n) using arrays (don't ask why I have to do so). The arrays for each number are made in the following way:

  • Their capacity is the number of digits of the greatest number between v and n (=q in the code)
  • vArray[i] = ith digit of v except leading zeros to fill the whole array
  • nArray[i] = - ith digit of n except leading zeros to fill the whole array

For example, choose v = 10 and n = 2 then,

vArray = [1,0]
nArray = [0,-2]

So I wrote this code to calculate the sum array that will be equal to the digits of the difference (sum = [0,9] for the example above):

long r = 0;
for (int i = q-1 ; i > -1; i--){
    sum[i] = vArray[i] + nArray[i];
    if (sum[i] < 0){
        r = floor(sum[i]/10);
        sum[i-1] -= r;
        sum[i] = sum[i]+10;
    }else{
        r = 0;
    }

    NSLog(@"%li",sum[i]);
}

The problem is that sum array isn't equal to what it should be. For the same example, sum = [1,8] What is the problem in the code?

note : vArray and nArray are properly generated.

EDIT : A few examples and expected results

    v =  |    n =   |  vArray =   |     nArray=    |    sum=
    25   |    9     |    [2,5]    |      [0,9]     |    [1,6]
    105  |    10    |   [1,0,5]   |     [0,1,0]    |   [0,9,5]
   1956  |   132    |  [1,9,5,6]  |    [0,1,3,2]   |  [1,8,2,4]
  369375 |   6593   |[3,6,9,3,7,5]|  [0,0,6,5,9,3] |[3,6,2,7,8,2]

解决方案

I believe I understand the data structure, as you are using a Big Integer representation.

Given the number: 1234

Your V array is: [1, 2, 3, 4].

To add all the digits (a.k.a. sum), which I don't see why you want to do this, is:

int digit_sum = 0;
for (int i = 0;  i < 4; i++)
{
    digit_sum += v[i];
} 

To convert the representation into "normal", try this:

int value = 0;
for (int i = 0; i < 4; ++i)
{
  value = (value * 10) + v[i];
}

To perform a subtraction, you will have to perform the steps as if you doing this by hand. Also, you would need a second number too.

Edit 1: link to big number subtraction
This might help:
Big Number Subtraction in C
C++ Large Number Arithmetic

这篇关于减去的数字使用数组 - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆