add: 220527 [cpp]

This commit is contained in:
Eat-Swap 2022-05-28 00:17:50 +08:00
parent d432178d1e
commit 8025bdd5b5
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 23 additions and 1 deletions

22
cpp/2205/220527.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <cstdio>
/**
* 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;
}

View File

@ -3,4 +3,4 @@ PROJECT(2205)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2205 220527-CN.cpp) ADD_EXECUTABLE(2205 220527.cpp)