diff --git a/cpp/2202/220225-CN.cpp b/cpp/2202/220225-CN.cpp new file mode 100644 index 0000000..64f6d14 --- /dev/null +++ b/cpp/2202/220225-CN.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +/** + * 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 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; +} diff --git a/cpp/2202/CMakeLists.txt b/cpp/2202/CMakeLists.txt index 7f7ca6f..7b782db 100644 --- a/cpp/2202/CMakeLists.txt +++ b/cpp/2202/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2202) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2202 220224.cpp) +ADD_EXECUTABLE(2202 220225-CN.cpp)