I have two arrays of integers and I want to multiply them together like the following:
我有两个整数数组,我想将它们相乘如下:
int[] arr1 = {6, 1}; // This represents the number 16
int[] arr2 = {4, 2}; // This represents the number 24
I want to store them in a new array so that the product appears as:
我想将它们存储在一个新数组中,以便产品显示为:
int[] productArray = {4, 8, 3};
I get how to do it with multiplying numbers like 2 x 24 since I can just push those into values into the product array. But when it comes to expanding beyond a single digit I am lost.
我得到了如何使用乘以2 x 24的数字来实现它,因为我可以将这些数字推入产品数组中。但是,当涉及扩展超过一位数时,我迷失了。
3 个解决方案
#1
3
Why do you need to do this? Regardless, here is an example:
你为什么需要这样做?无论如何,这是一个例子:
int total1; // to find what the array represents in #
for (int c = 0; c < arr1.length() - 1; c++) {
total1 += arr1[c] * Math.pow(10, (c+1)); // takes the number and adds it to the decimal number it should be
}
int total2;
for (int c = 0; c < arr2.length() - 1; c++) {
total1 += arr2[c] * Math.pow(10, (c+1));
}
String s = Integer.toString(total2*total1);
for (int c = s.length()-1; c >= 0; c--) { // adds int to array backwards
productArray.add(Integer.parseInt(s.atIndex(c)));
}
Notes: I have not bug tested this or run it through the JVM. Java isn't my usual programming language so I may have made a few mistakes. the "productArray" needs to be an ArrayList<> instead. I suspect that Integer.parseInt() is only for strings, so you may have to search for the char version of the function. Also, you need include Math...
注意:我没有对此进行错误测试或通过JVM运行它。 Java不是我常用的编程语言,所以我可能犯了一些错误。 “productArray”需要是一个ArrayList <>。我怀疑Integer.parseInt()仅用于字符串,因此您可能必须搜索该函数的char版本。此外,你需要包括数学......
#2
3
int arr1num, arr2num, product;
multiplier = 1;
for (i = arr1.size();i>0;i--){
arr1num = arr1num + (arr1[i] * multiplier);
multiplier = multiplier * 10;
}
--do this also for the second array
- 这也是第二个阵列
-- now we have arr1num and arr2num as the numbers of both arrays and just get the product
- 现在我们将arr1num和arr2num作为两个数组的数量,然后得到产品
product = arr1num * arr2num;
-- now store it in an array
- 现在将它存储在一个数组中
int divisor = 10;
int number;
for(i=0;i<int.length();i++){
number = product % divisor;
productArray.add(number);
divisor = divisor * 10;
}
#3
1
You could use this (although it's not the best algorithm you could use to do this):
您可以使用它(尽管它不是您可以用来执行此操作的最佳算法):
public static int[] multiplyArrays(int[] a, int[] b){
//turns arrays into integers
String num1 = "";
for (int i = a.length - 1; i > -1; i--){
num1 += a[i];
}
String num2 = "";
for (int i = b.length - 1; i > -1; i--){
num2 += b[i];
}
//does calculation
char[] answer = Integer.toString(Integer.parseInt(num1) * Integer.parseInt(num2)).toCharArray();
//converts char array into int array
int[] answer2 = new int[answer.length];
for (int i = 0; i < answer.length; i++){
answer2[answer.length - i - 1] = Integer.parseInt(String.valueOf(answer[i]));
}
return answer2;
}
#1
3
Why do you need to do this? Regardless, here is an example:
你为什么需要这样做?无论如何,这是一个例子:
int total1; // to find what the array represents in #
for (int c = 0; c < arr1.length() - 1; c++) {
total1 += arr1[c] * Math.pow(10, (c+1)); // takes the number and adds it to the decimal number it should be
}
int total2;
for (int c = 0; c < arr2.length() - 1; c++) {
total1 += arr2[c] * Math.pow(10, (c+1));
}
String s = Integer.toString(total2*total1);
for (int c = s.length()-1; c >= 0; c--) { // adds int to array backwards
productArray.add(Integer.parseInt(s.atIndex(c)));
}
Notes: I have not bug tested this or run it through the JVM. Java isn't my usual programming language so I may have made a few mistakes. the "productArray" needs to be an ArrayList<> instead. I suspect that Integer.parseInt() is only for strings, so you may have to search for the char version of the function. Also, you need include Math...
注意:我没有对此进行错误测试或通过JVM运行它。 Java不是我常用的编程语言,所以我可能犯了一些错误。 “productArray”需要是一个ArrayList <>。我怀疑Integer.parseInt()仅用于字符串,因此您可能必须搜索该函数的char版本。此外,你需要包括数学......
#2
3
int arr1num, arr2num, product;
multiplier = 1;
for (i = arr1.size();i>0;i--){
arr1num = arr1num + (arr1[i] * multiplier);
multiplier = multiplier * 10;
}
--do this also for the second array
- 这也是第二个阵列
-- now we have arr1num and arr2num as the numbers of both arrays and just get the product
- 现在我们将arr1num和arr2num作为两个数组的数量,然后得到产品
product = arr1num * arr2num;
-- now store it in an array
- 现在将它存储在一个数组中
int divisor = 10;
int number;
for(i=0;i<int.length();i++){
number = product % divisor;
productArray.add(number);
divisor = divisor * 10;
}
#3
1
You could use this (although it's not the best algorithm you could use to do this):
您可以使用它(尽管它不是您可以用来执行此操作的最佳算法):
public static int[] multiplyArrays(int[] a, int[] b){
//turns arrays into integers
String num1 = "";
for (int i = a.length - 1; i > -1; i--){
num1 += a[i];
}
String num2 = "";
for (int i = b.length - 1; i > -1; i--){
num2 += b[i];
}
//does calculation
char[] answer = Integer.toString(Integer.parseInt(num1) * Integer.parseInt(num2)).toCharArray();
//converts char array into int array
int[] answer2 = new int[answer.length];
for (int i = 0; i < answer.length; i++){
answer2[answer.length - i - 1] = Integer.parseInt(String.valueOf(answer[i]));
}
return answer2;
}