【字符串】537. 复数乘法

题目:

 

 

 

解答:

 

 

 1 class Solution {
 2 public:
 3     string complexNumberMultiply(string a, string b) 
 4     {
 5         int a1 = stoi(a);
 6         int b1 = stoi(b);
 7 
 8         int i = 0;
 9         int j = 0;
10 
11         while (a[i] != '+') 
12         {
13             i++;
14         }
15         while (b[j] != '+')
16         {
17             j++;
18         }
19 
20         a = a.substr(i + 1);
21         b = b.substr(j + 1);
22         int a2 = stoi(a);
23         int b2 = stoi(b);
24 
25         string res1 = to_string(a1 * b1 - a2 * b2);
26         string res2 = to_string(a1 * b2 + a2 * b1);
27         
28         return res1 + "+" + res2 + "i";
29     }
30 };

 

 

posted @ 2020-05-04 12:10  梦醒潇湘  阅读(174)  评论(0)    收藏  举报