算法之位运算
分析和思路:遍历数字的每一位,遇到1的时候,进入while循环,用一个变量保存最大值,并更新
1 #include <iostream> 2 #include "iostream" 3 4 using namespace std; 5 6 int main() 7 { 8 int number = 0; 9 while (cin >> number) 10 { 11 int a = 1; 12 13 int max = 0; 14 while (a <= number) 15 {//2 16 int count = 0; 17 if (number&a) 18 {//1 19 while (number&a) 20 { 21 count++; 22 a = a << 1; 23 24 } 25 if (max < count) 26 { 27 max = count; 28 } 29 }//1 30 else 31 { 32 a = a << 1; 33 } 34 }//2 35 cout << max << endl; 36 } 37 38 return 0; 39 40 }
主要为了自己学习