diff --git a/cpp/2205/220527.cpp b/cpp/2205/220527.cpp new file mode 100644 index 0000000..eefcf7a --- /dev/null +++ b/cpp/2205/220527.cpp @@ -0,0 +1,22 @@ +#include + +/** + * 1342. Number of Steps to Reduce a Number to Zero + * Given an integer num, return the number of steps to reduce it to zero. + * In one step, if the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it. + * + * Refer: 220131-CN.cpp + * __builtin functions this time. + */ + +class Solution { +public: + static int numberOfSteps(int num) { + return num ? 31 - __builtin_clz(num) + __builtin_popcount(num) : 0; + } +}; + +int main() { + std::printf("%d\n", Solution::numberOfSteps(14)); + return 0; +} diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index 2a5d483..6fafc44 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 220527-CN.cpp) +ADD_EXECUTABLE(2205 220527.cpp)