【发布时间】:2019-05-04 15:54:12
【问题描述】:
目前我开始阅读一本关于 alogirthms 的书,所以我现在正在尝试一些非常简单的算法来适应转换等等。在这个小班上,我想启用一个带有进位的学校添加功能。
如何将生成的 Int 数组转换为 int?我不知道如何将它们转换为足够的..
当前结果是 [7, 8, 3, 7, 9, 0, 5, 6],我想将这些数字连接成 (78379056) 的一个整数。我有哪些可能性?
public class Addition {
public int[] addiere(int[] a, int[] b) {
int res = 0;
int[] c = new int[a.length];
for(int i = a.length-1 ; i>=0 ; i--) {
res = a[i]+b[i];
if(res>=10) {
c[i] = oneValue(res);
a[i-1]+=1;
} else c[i]=res;
System.out.println("a -- "+a[i]+" b -- "+b[i]+" c -- "+c[i]);
}
return c;
}
public int oneValue(int t) {
String res;
int val;
res=Integer.toString(t);
res = res.substring(res.length()-1);
val = Integer.parseInt(res);
return val;
}
public static void main(String[] args) {
int[] a = {3,4,6,8,9,1,2,4};
int[] b = {4,2,5,7,8,9,3,2};
Addition add = new Addition();
int[] result;
//returns an array of Integers
System.out.println(Arrays.toString(add.addiere(a, b)));
result = add.addiere(a, b);
//HERE should be a method to convert the result ( Array of Integers ) just into a normal integer
}
}
【问题讨论】:
-
请在您的问题中添加完整的问题陈述。这种方法应该做什么?样本输入输出是什么?
-
两个数组的长度是否相同?如果输入是
{9}, {9},会发生什么?数组可以有2位数字还是3位数字? -
@SMA 我首先只是寻找基本情况,其中解析的数字是从 0 到 9 的整数。扩展可能会在以后出现。但我期待的是
{9}, {9}案例。您有什么改进建议吗?