-
[JS] 비밀지도이노베이션 캠프/3주차_알고리즘 2023. 6. 21. 14:24
문제
https://school.programmers.co.kr/learn/courses/30/lessons/17681
문제 풀이
function changBin(num, n) { // 10진수를 2진수로 변경해주는 함수 let bin = num.toString(2); // 1) 2진수로 변경 (string형) let len = bin.length; // 2) 변경한 2진수의 길이 저장 if (len >= n) return bin; // 3) 2진수로 변경한 값이 입력된 길이 값과 동일한 경우 리턴 return "0".repeat(n-len) + bin; // 4) 길이 값이 모자른 경우 앞에 "0"으로 채워줌 } function solution(n, arr1, arr2) { let result = []; // 1) 결과 값을 저장할 변수 선인 및 할당 // 2) for문 사용하여 모든 배열의 요소에 접근 for (let i=0; i<arr1.length; i ++ ) { 2-2) 10진수를 2진수로 변경 let num1 = changBin(arr1[i], n); let num2 = changBin(arr2[i], n); let temps = []; // 2-3) for문 사용하여 이진수 값 하나하나에 접근 for (let j=0; j<num1.length; j++) { let n1 = num1[j], n2 = num2[j]; // 2-4) 둘 중 하나라도 1인 경우 #, 둘다 공백인 경우는 " " if (n1 === "1" || n2 === "1") temps.push("#"); else temps.push(" "); } result.push(temps.join("")) } return result; }
728x90'이노베이션 캠프 > 3주차_알고리즘' 카테고리의 다른 글
[JS] 크레인 인형뽑기 게임 (0) 2023.06.21 [JS] 다트 게임 (1차) (0) 2023.06.21 [JS] 폰켓몬 (0) 2023.06.21 [JS] 소수 찾기 (0) 2023.06.21 [JS] 나머지가 1이 되는 수 찾기 (0) 2023.06.21