java 字符串乘法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4446326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
string multiplication
提问by Jony
I am trying to multiply two strings, but I am getting the wrong answer. Any help will be appreciated:
我试图将两个字符串相乘,但我得到了错误的答案。任何帮助将不胜感激:
public class stringmultiplication {
public static void main(String[] args) {
String s1 = "10";
String s2 = "20";
int num = 0;
for(int i = (s1.toCharArray().length); i > 0; i--)
for(int j = (s2.toCharArray().length); j > 0; j--)
num = (num * 10) + ((s1.toCharArray()[i - 1] - '0') * (s2.toCharArray()[j - 1] - '0'));
System.out.println(num);
}
}
采纳答案by Petro Semeniuk
public static void main(String[] args) {
String number1 = "108";
String number2 = "84";
char[] n1 = number1.toCharArray();
char[] n2 = number2.toCharArray();
int result = 0;
for (int i = 0; i < n1.length; i++) {
for (int j = 0; j < n2.length; j++) {
result += (n1[i] - '0') * (n2[j] - '0')
* (int) Math.pow(10, n1.length + n2.length - (i + j + 2));
}
}
System.out.println(result);
}
This one should be correct implementation without using integers.
这应该是不使用整数的正确实现。
回答by Matthew Flaschen
You're multiplying the numbers digit-wise, and you're not handling the powers of 10 correctly.
您正在按数字乘以数字,并且您没有正确处理 10 的幂。
You need to first parse the strings into integers. You're on the right track here. You can simplify the loop indices, and you only have to call toCharArray
once. E.g.:
您需要首先将字符串解析为整数。你在这里走在正确的轨道上。您可以简化循环索引,并且只需调用toCharArray
一次。例如:
After parsing, you can multiply the integers.
解析后,您可以将整数相乘。
EDIT: If that's not allowed, you need to implement an algorithm like this one, which is a bit more complicated.
编辑:如果这是不允许的,你需要实现类似的算法这一块,这是一个比较复杂一点。
One approach is to make an (n + 1) x (m + n) array (strictly an array of arrays), where m and n are the number of digits in each. It will be initialized to 0, and you can use this as an area to put the rows of the immediate and final results. These are then summed with carry. This is obviously a n?ive algorithm.
一种方法是创建一个 (n + 1) x (m + n) 数组(严格来说是一个数组数组),其中 m 和 n 是每个数组中的位数。它将被初始化为 0,您可以将此作为一个区域来放置立即和最终结果的行。然后将这些与进位相加。这显然是一种有效的算法。
E.g. for the example above:
例如上面的例子:
int[][] intermediates = new int[3][4];
This is an upper bound.
这是一个上限。
回答by asela38
Following is the solution which i suggest, what you forgot doing there is keeping the intermediate value.
以下是我建议的解决方案,您忘记在那里做什么是保持中间值。
public class T{
public static void main(String[] args) {
char[] num1 = "127".toCharArray();
char[] num2 = "32".toCharArray();
int[] intermediate = new int[num1.length];
for (int i = 0 ; i < num1.length ; i++ ) {
for(int j = 0 ; j < num2.length ; j++ ) {
int d1 = num1[num1.length - i - 1]-'0';
int d2 = num2[num2.length - j - 1]-'0';
intermediate[i] += d1 * d2 * (int) Math.pow(10,j);
System.out.printf(" %d X %d = %d\n", d1, d2, intermediate[i]);
}
intermediate[i] *= (int) Math.pow(10,i);
System.out.println(" intermediate : " + intermediate[i]);
}
int sum = 0;
for(int i : intermediate) {
sum += i;
}
System.out.println("Sum is = " + sum);
}
}
回答by Cole Murray
I found Peter's Algorithm using the pow function to be a bit confusing. Here is essentially the same algorithm. Convert your Strings to char[]'s and then run this.
我发现使用 pow 函数的 Peter 算法有点令人困惑。这里基本上是相同的算法。将您的字符串转换为 char[] 的字符串,然后运行它。
public static int multiply (char A[], char B[]){
int totalSum = 0, sum = 0;
for (int i = 0; i < A.length; i++){
sum = 0;
for (int j = 0; j < B.length; j++){
sum *= 10;
sum += (A[i] - '0') * (B[j] - '0');
}
totalSum *=10;
totalSum += sum;
}
return totalSum;
}