๋นํธ ์ฐ์ฐ์
์ ์ ํ์ ์ ํผ์ฐ์ฐ์์๋ง ์ ์ฉ๋ ์ ์๋ค.
๊ธฐ๋ณธ ์๋ฆฌ
- A^B๋ ๊ฐ์ด ๊ฐ์ผ๋ฉด 0 ๋ค๋ฅด๋ฉด 1์ ๋ฐํํ๋ค.
- ~A๋ -A-1๊ณผ ๊ฐ๋ค.
- ์ํํธ ์ฐ์ฐ์์์ ๋น ๋นํธ์ ๋ชจ๋ 0์ด ์ฑ์์ง์ง๋ง >> ์ฐ์ฐ์์์ A๊ฐ ์์์ธ ๊ฒฝ์ฐ์ 1์ด ์ฑ์์ง๋ค.
๋ฌธ์
https://tech.kakao.com/2017/09/27/kakao-blind-recruitment-round-1/
#include <iostream>
using namespace std;
int main()
{
int n = 0;
cout << "์ง๋ ํ ๋ณ์ ํฌ๊ธฐ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์: ";
cin >> n;
int* arr1 = new int[n];
int* arr2 = new int[n];
cout << "์ง๋ 1์ ์ ์ ๋ฐฐ์ด์ ์
๋ ฅํด์ฃผ์ธ์: ";
for (int i = 0; i < n; ++i)
cin >> arr1[i];
cout << "์ง๋ 2์ ์ ์ ๋ฐฐ์ด์ ์
๋ ฅํด์ฃผ์ธ์: ";
for (int i = 0; i < n; ++i)
cin >> arr2[i];
int* map = new int[n];
for (int i = 0; i < n; ++i)
map[i] = arr1[i] | arr2[i]; // ์ง๋ 1, 2๋ก ์ ์ฒด ์ง๋ ๋ง๋ค๊ธฐ
int d = 1 << (n - 1); // n์ด 4์ผ๋, 1000 -> ๊ฐ์ฅ ์ฒซ ๋ฒ์งธ ๋นํธ์ and ์ฐ์ฐ์ ํ๊ธฐ ์ํด
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (map[i] & d)
cout << "#";
else
cout << " ";
map[i] <<= 1; // ๋นํธ ์ผ์ชฝ์ผ๋ก 1 ์ด๋ ํ ํ ๋น
}
cout << endl;
}
return 0;
}
https://www.acmicpc.net/problem/11811
#include <iostream>
using namespace std;
int main(void)
{
int N;
cin >> N;
for (int i = 0; i < N; ++i) {
int a = 0, tmp;
for (int j = 0; j < N; ++j) {
cin >> tmp;
if (i != j)
a = a | tmp;
}
cout << a << " ";
}
return 0;
}
https://leetcode.com/problems/single-number-ii/description/
#include <vector>
using namespace std;
class Solution {
public:
int singleNumber(vector<int>& nums) {
int first = 0, second = 0;
for (int i = 0; i < nums.size(); ++i) {
first = (first ^ nums[i]) & (~second);
second = (second ^ nums[i]) & (~first);
}
return first;
}
};
https://leetcode.com/problems/bitwise-and-of-numbers-range/description/
class Solution {
public:
int rangeBitwiseAnd(int left, int right) {
while (left < right)
right = right & (right - 1);
return right;
}
};