[프로그래머스] [1차] 비밀지도

less than 1 minute read

문제 설명

https://programmers.co.kr/learn/courses/30/lessons/17681

제한사항


풀이

11분
2진법으로 변환하는 방법을 물어보는 문제였다.

코드

// 09:19 ~ 9:30
#include <string>
#include <vector>

using namespace std;

string decrypt(int n, int len){
    string s = "";
    while(n > 0){
        s = to_string(n%2) + s;
        n /= 2;
    }
    while(s.size() < len){
        s = '0' + s;
    }
    return s;
}

vector<string> solution(int n, vector<int> arr1, vector<int> arr2) {
    vector<string> answer;
    for(int i =0; i < arr1.size(); ++i){
        string s1 = decrypt(arr1[i], n);
        string s2 = decrypt(arr2[i], n);
        string temp = "";
        for(int j =0; j < s1.size(); ++j){
            if(s1.at(j) == '0' && s2.at(j) == '0') temp += ' ';
            else temp += '#';
        }
        answer.push_back(temp);
    }
    return answer;
}