add: 220527 [cpp]
This commit is contained in:
parent
d432178d1e
commit
8025bdd5b5
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue