diff --git a/cpp/2205/220530.cpp b/cpp/2205/220530.cpp new file mode 100644 index 0000000..2efbca1 --- /dev/null +++ b/cpp/2205/220530.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +/** + * 29. Divide Two Integers + * Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator. + * The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345 would be truncated to 8, and -2.7335 would be truncated to -2. + * Return the quotient after dividing dividend by divisor. + * - Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For this problem, if the quotient is strictly greater than 231 - 1, then return 231 - 1, and if the quotient is strictly less than -231, then return -231. + */ + +class Solution { +public: + static int divide(int dividend, int divisor) { + unsigned ans = 0, cur = 1; + bool neg = (dividend ^ divisor) & 0x80000000; + + unsigned r = 0x80000000 == divisor ? 0x80000000 : std::abs(divisor), + d = 0x80000000 == dividend ? 0x80000000 : std::abs(dividend); + + while (r < d) { + r <<= 1; + cur <<= 1; + } + + while (cur) { + if (d >= r) { + ans += cur; + d -= r; + } + cur >>= 1; + r >>= 1; + } + if (neg) + return ans > 0x80000000 ? (-2147483647 - 1) : -ans; + return ans > 0x7FFFFFFF ? 2147483647 : ans; + } +}; + +int main() { + Solution::divide(-2147483647 - 1, -1); + + std::mt19937 r(std::time(nullptr)); + + for (int N = 1048576; N--; ) { + int x = r(), y = r(); + int d1 = x / y, d2 = Solution::divide(x, y); + if (d1 != d2) { + std::printf("Bad answer: (%d / %d) -> %d, but %d is expected.\n", x, y, d2, d1); + return -1; + } + } + + return 0; +} diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index 87ab22a..8f5b42b 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220529-CN.cpp) +ADD_EXECUTABLE(2205 220530.cpp) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 04c9982..da2fd05 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -10,7 +10,7 @@ ELSE() ENDIF() IF(UNIX) - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer") ENDIF() # Optimisation