AtCoder Beginner Contest 336 (1)

本文介绍如何利用编程方法计算一个正整数N在二进制表示中连续零的个数(ctz函数),包括使用短除法和C++内置函数。示例展示了如何找出2024的ctz值为3。
摘要由CSDN通过智能技术生成

 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 consecutive 0s 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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值