给定两个表示复数的字符串。
返回表示它们乘积的字符串。注意,根据定义 i2 = -1 。
示例 1:
输入: "1+1i", "1+1i"
输出: "0+2i"
解释: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i ,你需要将它转换为 0+2i 的形式。
示例 2:
输入: "1+-1i", "1+-1i"
输出: "0+-2i"
解释: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i ,你需要将它转换为 0+-2i 的形式。
注意:
输入字符串不包含额外的空格。
输入字符串将以 a+bi 的形式给出,其中整数 a 和 b 的范围均在 [-100, 100] 之间。
输出也应当符合这种形式。
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/complex-number-multiplication 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public:
string complexNumberMultiply(string a, string b) {
int x1, y1, x2, y2, x, y;
getxy(a,x1,y1);
getxy(b,x2,y2);
x = x1*x2-y1*y2;
y = x1*y2+x2*y1;
string ans(to_string(x)+"+"+to_string(y)+"i");
return ans;
}
void getxy(string &s, int &x, int &y)
{
int sum = 0;
bool negative = false;
for(int i = 0; i < s.size(); ++i)
{
if(isdigit(s[i]))
sum = sum*10+s[i]-'0';
else
{
if(s[i] == '-')
negative = true;
else if(s[i] == '+')
{
x = negative == true ? -sum : sum;
negative = false;
sum = 0;
}
else// s[i] == 'i'
y = negative == true ? -sum : sum;
}
}
}
};