diff --git a/2201/220110.cpp b/2201/220110.cpp new file mode 100644 index 0000000..0fed99e --- /dev/null +++ b/2201/220110.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +class Solution { +public: + static std::string addBinary(const std::string& a, const std::string& b) { + char* const ret = new char[std::max(a.length(), b.length()) + 2]{}; + int cin = 0; + for (int pos = std::max(a.length(), b.length()), posA = a.length() - 1, posB = b.length() - 1; posA >= 0 || posB >= 0; --posA, --posB, --pos) { + ret[pos] = (((posA < 0 ? 0 : (a[posA] ^ 48)) + (posB < 0 ? 0 : (b[posB] ^ 48)) + cin) & 1) ^ 48; + cin = ((posA < 0 ? 0 : (a[posA] ^ 48)) + (posB < 0 ? 0 : (b[posB] ^ 48)) + cin) >> 1; + } + if (cin) + *ret = '1'; + std::string r(*ret ? ret : (ret + 1)); + delete[] ret; + return r; + } +}; + +int main() { + std::cout << Solution::addBinary("0", "1"); + return 0; +} diff --git a/2201/CMakeLists.txt b/2201/CMakeLists.txt index 278f84f..e510b1f 100644 --- a/2201/CMakeLists.txt +++ b/2201/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2201) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2201 220110-CN.cpp) \ No newline at end of file +ADD_EXECUTABLE(2201 220110.cpp) \ No newline at end of file