add: 220225-CN [cpp]

This commit is contained in:
Lam Haoyin 2022-02-25 01:03:29 +08:00
parent 395e0c060b
commit b58bbe02db
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 32 additions and 1 deletions

31
cpp/2202/220225-CN.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <complex>
#include <cstdio>
#include <string>
/**
* 537. Complex Number Multiplication
* A complex number can be represented as a string on the form "real+imaginaryi" where:
* real is the real part and is an integer in the range [-100, 100].
* imaginary is the imaginary part and is an integer in the range [-100, 100].
* i2 == -1.
* Given two complex numbers num1 and num2 as strings, return a string of the complex number that represents their multiplications.
*/
class Solution {
public:
static std::string complexNumberMultiply(const std::string& num1, const std::string& num2) {
int r1, r2, i1, i2;
std::sscanf(num1.c_str(), "%d+%d", &r1, &i1);
std::sscanf(num2.c_str(), "%d+%d", &r2, &i2);
std::complex<int> c1(r1, i1), c2(r2, i2);
auto ans = c1 * c2;
char ret[50]{};
std::sprintf(ret, "%d+%di", ans.real(), ans.imag());
return ret;
}
};
int main() {
// too simple to test
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2202)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2202 220224.cpp) ADD_EXECUTABLE(2202 220225-CN.cpp)