【发布时间】:2017-03-24 14:04:59
【问题描述】:
我的函数收到不一致的结果,该函数将减去 32 位不同长度的数组中的元素。它不是从两个数组的末尾开始,而是从第二个数组的开头开始一直到末尾。我对为什么有点困惑,因为我的循环是从最后开始的。任何帮助将不胜感激。
int carry = 0;
int total = Math.max(tempArray.length, tempArrayTwo.length);
int[] subArray = new int[total];
for (int i = differenceArray.length - 1; i >= 0; i--) {
int first = i < tempArray.length ? tempArray[tempArray.length - i - 1] : 0;
int second = i >= tempArrayTwo.length ? 0 : tempTwoArray[tempTwoArray.length - i - 1];
int difference = first - second - carry;
carry = 0;
if (difference < 0) {
difference = difference + 10;
System.out.println(difference);
carry = 1;
}
subArray[subArray.length - 1 - i] = difference;
我遇到的问题是说数组一的大小为 6,总数为 46,数组二的大小为 3(总数为 24)是减法将从第一个字符的第 24 个字符的开头开始数组和第二个数组的第一个元素。
(at the 24th of 46 slot of array One)
00003333 33333333 33333333 数组一
10000000 00000000 00000000 数组二
不是从arrayOne的3和arrayTwo的0开始,而是从arrayOne的0和arrayTwo的1开始。 很奇怪,特别是因为它有时会相应地工作。较大数组中的额外数字被向下传递,仅供参考。只是这些特定的数字被搞砸了。
【问题讨论】:
-
您在寻找
BigInteger吗? -
暂时没有,正在寻找我自己的实现。