문제 설명
범위 [L, R]이 주어지면 작업은 L ≤ X
예시
L =1이고 R =10이면 최대 비트 AND 값은 8이며 다음과 같이 구성할 수 있습니다. -
1000 # Binary representation of 8 Bitwise AND 1001 # Binary representation of 9 ---- 1000 # Final result
알고리즘
L에서 R까지 반복하고 가능한 모든 쌍에 대해 비트 AND를 확인하고 끝에 최대값을 인쇄합니다.
예시
이제 예를 살펴보겠습니다 -
#include <bits/stdc++.h> using namespace std; int getMaxBitwiseAndValue(int L, int R) { int maxValue = L & R; for (int i = L; i < R; ++i) { for (int j = i + 1; j <= R; ++j) { maxValue = max(maxValue, (i & j)); } } return maxValue; } int main() { int L = 1, R = 10; cout << "Maximum value = " << getMaxBitwiseAndValue(L, R) << endl; return 0; }
출력
Maximum value = 8