例题,求输出结果。
1 #include<iostream> 2 using namespace std; 3 int func(int x) 4 { 5 int count=0; 6 while(x) 7 { 8 count++; 9 x = x & ( x-1 ); 10 11 } 12 return count; 13 } 14 15 int main() 16 { 17 cout<<func(9999)<<endl; 18 return 0; 19 }
分析本题func函数可知其返回值是形参x转化为二进制后包含1的数量。
9999转化为二进制为:10011100001111
故程序输出结果为8.
浙公网安备 33010602011771号