
배열이 주어진 문제를 해결하려면 최소한 비어 있지 않은 하위 배열의 비트 AND인 가능한 모든 정수를 찾아야 합니다. 간단한 방법
은 비어 있지 않은 가능한 모든 하위 배열을 찾는것입니다.
Input : nums[ ] = { 3, 5, 1, 2, 8 }
Output : { 2, 5, 0, 3, 8, 1 }
Explanation:
2 is the bitwise AND of subarray {2},
5 is the bitwise AND of subarray {5},
0 is the bitwise AND of subarray {1, 2}, {2, 8} and {1, 2, 8},
3 is the bitwise AND of subarray {3},
8 is the bitwise AND of subarray {8},
1 is the bitwise AND of subarray {1}, {3, 5} and {3, 5, 1}.
Input : nums[ ] = { 2, 6, 3, 8, 1 }
Output: { 1, 8, 3, 6, 2, 0 }
#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[] ={ 2, 6, 3, 8, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
// Declaring set to store result of each AND operation.
unordered_set<int> result;
int val;
// nested loops to traverse through all the possible non empty subarrays.
for (int i = 0; i < n; ++i){
for (int j = i, val = INT_MAX; j < n; ++j){
val = val & arr[j];
// storing result of AND operation
result.insert(val);
}
}
cout << "All possible numbers are: ";
// printing all the values of set.
for (auto i = result.begin(); i != result.end();i++)
cout << *i << " ";
return 0;
}위 내용은 C++로 작성된 하나 이상의 비어 있지 않은 하위 배열을 포함하는 숫자의 비트별 AND의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!