안녕 세상아,

[프로그래머스/c++] Lv1 비밀지도 본문

프로그래머스

[프로그래머스/c++] Lv1 비밀지도

돈 많은 백수가 되고싶다 2024. 9. 20. 15:49

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

1. 십진수를 이진수로 바꾸는 함수이다. 

2. vector 하나씩 각각 비교해서 십진수를 이진수로 만든다.

3. 만든 후 이진수를 하나씩 비교해서 둘 다 0일 때만 " " 추가한다.

4. 나머지는 다 "#" 추가한다.

5. 이를 ans에 넣어두고 완료되면 answer에 push_back 한다. 

#include <string>
#include <algorithm>
#include <vector>

using namespace std;

string tenToTwo(int n, int size) {
    string bit = "";
    while (n != 0) {
        bit += to_string(n % 2);
        n = n / 2;
    }
    reverse(bit.begin(), bit.end());
    while(bit.size()<size){
        bit = "0" + bit;
    }
    return bit;
}
vector<string> solution(int n, vector<int> arr1, vector<int> arr2) {
    vector<string> answer;

    //우선 2진수로 바꾸고 (한줄씩)
    //비교해서 만약 1이 있으면 무조건 # 넣고 둘 다 0일 때만 비워놓기
    for (int i = 0; i < n; i++) {
        int x1 = arr1[i];
        int x2 = arr2[i];

        string y1 = tenToTwo(x1,n);
        string y2 = tenToTwo(x2,n);

        string ans = "";
        for (int j = 0; j < y1.size(); j++) {
            if (y1[j] == '0' && y2[j] == '0') {
                ans += ' ';
            }
            else {
                ans += '#';
            }
        }
        answer.push_back(ans);
    }

    return answer;
}

 

비트연산자 사용해서 풀면 더 간단하게 풀 수 잇는 것 같다.. 비트 연산자..메모..공부할 것..