B - CTZ Editorial
Time Limit: 2 sec / Memory Limit: 1024 MB
Score: 200200 points
Problem Statement
For a positive integer �X, let ctz(�)ctz(X) be the (maximal) number of consecutive zeros at the end of the binary notation of �X.
If the binary notation of �X ends with a 11, then ctz(�)=0ctz(X)=0.
You are given a positive integer �N. Print ctz(�)ctz(N).
Constraints
- 1≤�≤1091≤N≤109
- �N is an integer.
Sample Input 1Copy
Copy
2024
Sample Output 1Copy
Copy
3
20242024 is
11111101000
in binary, with three consecutive0
s from the end, so ctz(2024)=3ctz(2024)=3.
Thus, print 33.
方法1,使用二进制转换短除法来做题
#include<iostream>
using namespace std;
int main()
{
int N,cnt;
cnt=0;
cin>>N;
for(int i=0;;i++)
{
if(N%2==0)
{cnt++;
N=N/2;
}
else
break;
}
cout<<cnt;
return 0;
}
方法2,使用c++函数
#include<iostream>
using namespace std;
#define int long long
int n;
signed main()
{
cin >> n;
cout << __builtin_ctz(n);
return 0;
}