From 8025bdd5b5b2c948a11b414b15c014eea6d21417 Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Sat, 28 May 2022 00:17:50 +0800 Subject: [PATCH] add: 220527 [cpp] --- cpp/2205/220527.cpp | 22 ++++++++++++++++++++++ cpp/2205/CMakeLists.txt | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 cpp/2205/220527.cpp 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)