gpt4 book ai didi

Java:如何对两个不同长度的数组的元素求和

转载 作者:行者123 更新时间:2023-11-29 05:34:25 25 4
gpt4 key购买 nike

我想把两个不同长度的数组的元素加在一起。下面的代码仅适用于相同的长度,这是我目前所拥有的。

    //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 最终将由用户输入。

好吧,你可以看出输入是有限的,因为如果 num1[0] + num2[0] > 9 进位没有要放置的索引,则无法编译。因此,我需要将整个数组向右移动并将进位从 num1[0] + num2[0] 放置。问题来了!!我应该把转移码放在哪里?我有点糊涂了......

最佳答案

实际上,您声明了一个容量为 Math.max(num1.length, num2.length) 的 int[] 数组。

还不够。您应该将容量设置为 Math.max(num1.length, num2.length) +1。

为什么?

看num1是否为{1,9,9,9},num2是否为{9,9,9,9}arraySum如何表示总和 {1,1,9,9,8}?

所以我们需要声明如下,考虑是否需要carry

 int[] arraySum = new int[capacity + 1];

然后在打印sum的时候,检查arraySum[0]是0还是1,如果等于0,就不要在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}为例,求和结果如下:

控制台输出:

11998

关于Java:如何对两个不同长度的数组的元素求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20028834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com