알고리즘(C++)/프로그래머스 level1
[1차] 비밀지도 - 2018 KAKAO BLIND RECRUITMENT
코딩고구마
2020. 4. 3. 15:21
https://programmers.co.kr/learn/courses/30/lessons/17681
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#include <string>
#include <vector>
#include <iostream>
using namespace std;
string decoding(int summed_value, int n) {
string ret;
int cmp = 1 << n-1;
for (int i = 0; i < n; i++) {
if (summed_value & cmp) {
ret.append("#");
}
else {
ret.append(" ");
}
summed_value = summed_value << 1;
}
return ret;
}
vector<string> solution(int n, vector<int> arr1, vector<int> arr2) {
vector<string> answer;
for (int i = 0; i < n; i++) {
int summed_value = arr1[i] | arr2[i];
answer.push_back(decoding(summed_value, n));
}
return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1. 지도로 주어진 두 arr에 대해 OR 비트연산( | )을 수행해 수들을 합쳐준다
2. 합쳐진 arr를 바탕으로 각 수를 비트단위로 체크해 1인 부분을 #, 0인 부분을 공백으로 문자열 생성
3. 비트 쉬프트 연산(<<) 을 n만큼 수행하면서 1과 AND 비트연산( & )을 통해 비트 정보 체크

반응형