演算法之位運算
阿新 • • 發佈:2022-03-05
分析和思路:遍歷數字的每一位,遇到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 }