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