
Complex Number Multiplication
给出两个a+bi 格式的数 求乘积.
i^2 = -1
例子
1 2 3
|
Input: "1+-1i", "1+-1i" Output: "0+-2i" Explanation: (1 - i) * (1 - i) = 1 + i^2 - 2 * i = -2i, and you need convert it to the form of "0+-2i".
|
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public class { public String complexNumberMultiply(String a, String b) { String[] arrayA = a.split("\+"); String[] arrayB = b.split("\+"); int a1 = Integer.parseInt(arrayA[0]); int a2 = Integer.parseInt(arrayA[1].substring(0, arrayA[1].length()-1)); int b1 = Integer.parseInt(arrayB[0]); int b2 = Integer.parseInt(arrayB[1].substring(0, arrayB[1].length()-1)); int c1 = a1*b1 - a2*b2; int c2 = a1*b2 + b1*a2; return c1+"+" + c2 + "i"; } }
|
近期评论