[Huawei Machine Test exercises]46. To calculate the number of 0 of binary numbers

Source: Internet
Author: User

Topic

Describe:

输入一个10进制数字,请计算该数字对应二进制中0的个数,注意左数第一个1之前的所有0都不需要计算。不需要考虑负数的情况。

Topic Category:

Difficulty:

Run time limit:

无限制

Memory Limit:

无限制

Stage:

Input:

要计算的十进制非负数

Output:

二进制中第一个1之后0 的个数

Sample input:

2

Sample output:

1

Code

/* ---------------------------------------* Date: 2015-07-03* sjf0115* title: Count the number of 0 of binary numbers * Source: Huawei Machine Test Exercises----------------- ------------------------*/#include <iostream>#include <vector>using namespace STD;//Decimal n corresponds to the number of binary in 0intZerocount (intN) {if(N <0){return 0; }//if    if(n = =0){return 1; }//if    intTMP = n;intCount =0;//Cycle right shift to determine the number of 0     while(TMP) {if(TMP &1) ==0) {++count; }//ifTMP = TMP >>1; }//while    returnCount;}intMain () {intN//freopen ("C:\\users\\administrator\\desktop\\c++.txt", "R", stdin);     while(Cin>>n) {cout<<zerocount (n) <<endl; }//while    return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Huawei Machine Test exercises]46. To calculate the number of 0 of binary numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.