使用数组 - C ++减去数字 [英] Subtract numbers using arrays - C++
问题描述
我想计算两个数字之间的差异(让我们说 v
和 n
,因此 vn
)使用数组(不要问为什么我必须这样做)。每个数字的数组以下列方式产生:
- 它们的容量是<$ c $代码中的
v 和 n
(=q
) -
vArray [i]
=i code>除了前导零填充整个数组
-
nArray [i]
= -i除了前导零填充整个数组之外,
数字n
的
例如,选择 v
= 10和 n
= 2然后,
vArray = [1,0]
pre>
nArray = [0,-2]
所以我写了这段代码来计算
sum
数组,sum = [0,9]
上面的示例):长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]);
}
问题是sum数组不等于它应该是什么。对于同一个示例,
sum = [1,8]
代码中有什么问题?
正确生成
vArray
和nArray
。
一些示例和预期结果
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]
解决方案我相信我理解数据结构,因为你使用的是一个大整数表示。
给定数字:1234
您的V数组是:[1,2,3,4]。
要添加所有数字(aka sum),我不知道为什么要这样做:
int digit_sum = 0;
for(int i = 0; i <4; i ++)
{
digit_sum + = v [i];
}
要将表示转换为normal,请尝试:
int value = 0;
for(int i = 0; i <4; ++ i)
{
value =(value * 10)+ v [i]
}
要执行减法,您必须执行步骤,这手工。
编辑1:链接到大数字减法
这可能有助于:
C中的大数字减法
C ++大数算术I want to calculate the difference between two numbers (let's say
v
andn
, sov-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
andn
(=q
in the code) vArray[i]
=i
th digit ofv
except leading zeros to fill the whole arraynArray[i]
= -i
th digit ofn
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屋!