https://school.programmers.co.kr/learn/courses/30/lessons/17681
그림을 보면 두 맵 중 하나라도 #이 존재할 경우 결과물에서도 #이 존재한다. (OR 조건)
정수 배열의 원소 x는 최대 2^16-1임을 파악할 수 있다.
10진수를 2진수로 바꾸고, 2진수를 문자열로 바꿔 담은 두 배열을 서로 비교하며 최종 결과물을 반환하는 문제이다.
처음부터 하나씩 계산되는 나머지를 역순하면 2진수임을 알 수 있다.
만약 9처럼 자릿수가 n보다 부족할 경우 그만큼 0을 채워주어야 하는 것을 잊지 말자.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> solution(int n, vector<int> arr1, vector<int> arr2) {
vector<string> answer;
vector<string> map1;
vector<string> map2;
string s = "";
for(int i=0; i<n; i++){ // arr1 10진수 2진수로 변환
int tmp = arr1[i];
while(tmp != 0){
s += (tmp % 2 + '0');
tmp /= 2;
}
for(int t=s.size(); t<n; t++) s += '0'; // 자릿수 부족할 경우 0으로 채워줘야 함
reverse(s.begin(), s.end());
map1.push_back(s);
s = "";
}
for(int i=0; i<n; i++){ // arr2 10진수 2진수로 변환
int tmp = arr2[i];
while(tmp != 0){
s += (tmp % 2 + '0');
tmp /= 2;
}
for(int t=s.size(); t<n; t++) s += '0'; // 자릿수 부족할 경우 0으로 채워줘야 함
reverse(s.begin(), s.end());
map2.push_back(s);
s = "";
}
for(int i=0; i<n; i++){ // map1와 map2를 비교하며 #와 공백을 채워나간다.
string s = "";
string a = map1[i];
string b = map2[i];
for(int j=0; j<n; j++){
if(a[j] == '1' || b[j] == '1') s += '#';
else s += ' ';
}
answer.push_back(s);
}
return answer;
}