RaFiN_

leetcode complex number multiplication

Dec 31st, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string complexNumberMultiply(string a, string b) {
  4.         int ra, ia, rb, ib;
  5.         char buff;
  6.         stringstream aa(a), bb(b), ans;
  7.         aa >> ra >> buff >> ia >> buff;
  8.         bb >> rb >> buff >> ib >> buff;
  9.         ans << ra*rb - ia*ib << "+" << ra*ib + rb*ia << "i";
  10.         return ans.str();
  11.     }
  12. };
  13. or
  14. string complexNumberMultiply(string a, string b) {
  15.     int a1 = 0, b1 = 0, a2 = 0, b2 = 0;
  16.     sscanf(a.c_str(), "%d+%di",&a1,&b1);
  17.     sscanf(b.c_str(), "%d+%di",&a2,&b2);
  18.     return to_string(a1*a2-b1*b2)+"+"+to_string(a1*b2+a2*b1)+"i";
  19. }
Add Comment
Please, Sign In to add comment