Java:如何求和两个长度不同的数组的元素 [英] Java: How to sum the elements of two arrays with different lengths
问题描述
我正在尝试将具有不同长度的两个数组的元素加在一起.下面的代码仅适用于相同的长度,这是我到目前为止所拥有的全部.
I am trying to add the elements of two arrays with different lengths together. The code below is only for the same length and here is all I have so far.
//for the same lengths
int[]num1 = {1,9,9,9};
int[]num2 = {7,9,9,9};// {9,9,9}
int total = 0, carry = 1;
int capacity = Math.max(num1.length,num2.length);
int []arraySum = new int [capacity];
for (int i = capacity - 1 ; i >= 0; i--)
{
arraySum[i] = num1[i]+ num2[i];
if (arraySum[i] > 9)
{
arraySum[i] = arraySum[i] % 10;
num2[i-1] = num2[i-1] + carry;
}
}
for(int i = 0; i < arraySum.length; i++)
{
System.out.print(arraySum[i]);
}
如果我将num2和length中的元素更改为{9,9,9},该怎么办?我知道我可能需要将另一个for循环作为内部for循环并以较小的长度控制数组的索引,但是如何.... ??还有一件事...对于那些for循环条件,我该怎么办,因为num1和num2最终将由用户输入.
What should I do if I change the elements in num2 and length to like {9,9,9}? I know I probably need to put another for-loop as an inside for-loop and control the indices of the array with smaller length but how....?? One more thing... what should I do for those for-loops conditions because num1 and num2 will be eventually INPUTED by the user.
好吧,您可以说输入是有限的,因为如果num1 [0] + num2 [0]> 9进位没有放置索引,则无法编译.因此,我需要将整个数组向右移,并将进位从num1 [0] + num2 [0]移出.这是问题所在!我应该把转换代码放在哪里?我有点困惑.......
Well, you can tell that the inputs are limited because if num1[0] + num2[0] > 9 the carry has no index to be placed, then it can't be compiled. So, I need to shift the whole array to the right and place the carry from num1[0] + num2[0]. Here is the problem!! Where should I put the shifting code? I am kinda confused.......
推荐答案
实际上,您声明了一个int []数组,其容量为 Math.max(num1.length,num2.length)
.
Actually, you declare an int[] array with the capacity as Math.max(num1.length, num2.length)
.
没有咳嗽.您应该将容量设置为Math.max(num1.length,num2.length)+1.
It is not encough. You should set the capacity as Math.max(num1.length, num2.length) +1.
为什么?
查看num1是否为 {1,9,9,9}
和num2是否为 {9,9,9,9}
,如何将arraySum代表总和{1,1,9,9,8}?
See if num1 is {1,9,9,9}
and num2 is {9,9,9,9}
, how can the arraySum to represent the sum {1,1,9,9,8}?
因此,我们需要按以下说明进行声明,以考虑是否需要携带.
So we need to declare it as below to consider if carry is needed.
int[] arraySum = new int[capacity + 1];
然后在打印总和时,请检查arraySum [0]是0还是1,如果它等于0,则不要在控制台中打印它.
Then when print the sum, check if arraySum[0] is 0 or 1, if it euqals to 0, do not print it in Console.
修改的参考代码如下:
包装问题;
public class Example {
public static void main(String[] args) {
// for the same lengths
int[] num1 = { 1,9,9,9 };
int[] num2 = { 9,9,9,9};// {9,9,9}
// 1999+9999 = 11998, its length is greater than the max
int capacity = Math.max(num1.length, num2.length);
int[] arraySum = new int[capacity + 1];
int len2 = num2.length;
int len1 = num1.length;
if (len1 < len2) {
int lengthDiff = len2 - len1;
/*
* Flag for checking if carry is needed.
*/
boolean needCarry = false;
for (int i = len1 - 1; i >= 0; i--) {
/**
* Start with the biggest index
*/
int sumPerPosition =0;
if (needCarry) {
sumPerPosition = num1[i] + num2[i + lengthDiff] +1;
needCarry = false;
}else
{
sumPerPosition = num1[i] + num2[i + lengthDiff];
}
if (sumPerPosition > 9) {
arraySum[i + lengthDiff + 1] = sumPerPosition % 10;
needCarry = true;
}else
{
arraySum[i + lengthDiff + 1] = sumPerPosition % 10;
}
}
/**
* Handle the remaining part in nun2 Array
*/
for (int i = lengthDiff - 1; i >= 0; i--) {
/*
* Do not need to care num1 Array Here now
*/
if(needCarry){
arraySum[i + 1] = num2[i]+1;
}else
{
arraySum[i + 1] = num1[i] ;
}
if (arraySum[i + 1] > 9) {
arraySum[i + 1] = arraySum[i + 1] % 10;
needCarry = true;
} else {
needCarry = false;
}
}
/*
* Handle the last number, if carry is needed. set it to 1, else set
* it to 0
*/
if (needCarry) {
arraySum[0] = 1;
} else {
arraySum[0] = 0;
}
} else {
int lengthDiff = len1 - len2;
/*
* Flag for checking if carry is needed.
*/
boolean needCarry = false;
for (int i = len2 - 1; i >= 0; i--) {
/**
* Start with the biggest index
*/
int sumPerPosition = 0;
if (needCarry) {
sumPerPosition = num2[i] + num1[i + lengthDiff] +1;
needCarry = false;
}else
{
sumPerPosition = num2[i] + num1[i + lengthDiff];
}
if (sumPerPosition > 9) {
arraySum[i + lengthDiff + 1] = sumPerPosition % 10;
needCarry = true;
}else
{
arraySum[i + lengthDiff + 1] = sumPerPosition % 10;
}
}
/**
* Handle the remaining part in nun2 Array
*/
for (int i = lengthDiff - 1; i >= 0; i--) {
/*
* Do not need to care num1 Array Here now
*/
if(needCarry){
arraySum[i + 1] = num1[i]+1;
}else
{
arraySum[i + 1] = num1[i] ;
}
if (arraySum[i + 1] > 9) {
arraySum[i + 1] = arraySum[i + 1] % 10;
needCarry = true;
} else {
needCarry = false;
}
}
/*
* Handle the last number, if carry is needed. set it to 1, else set
* it to 0
*/
if (needCarry) {
arraySum[0] = 1;
} else {
arraySum[0] = 0;
}
}
/*
* Print sum
*
* if arraySum[0] ==1, print 1
*
* Do not print 0 when arraySum[0] ==0
*/
if(arraySum[0] == 1)
{
System.out.print(1);
}
for (int i = 1; i < arraySum.length; i++) {
System.out.print(arraySum[i]);
}
}
}
示例num1为{1,9,9,9}而num2为{9,9,9,9},求和结果如下:
An example that num1 is {1,9,9,9} and num2 is {9,9,9,9}, the sum result is as follows:
控制台中的输出:
11998
这篇关于Java:如何求和两个长度不同的数组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!