Algorithm/JS 7

[알고리즘_Node.js_브루트 포스] 백준 7568번 덩치

몸무게와 키 배열을 따로 만들고 정답 배열의 초기값을 1등으로 시작했습니다. 그리고 배열을 돌아가면서 몸무게와 키 모두 값이 적을때 +1을 해주면서 등수가 밀리도록 했습니다. const fs = require("fs"); const [n, ...arr] = fs.readFileSync("/dev/stdin").toString().trim().split(/\n/); const height = []; const weight = []; const answer = []; for (let i = 0; i < n; i++) { const newArr = arr[i].split(" ").map(Number); weight.push(newArr[0]); // 몸무게 배열 height.push(newArr[1]); //..

Algorithm/JS 2022.09.13

[알고리즘_Node.js_재귀] 백준 11729번 하노이 탑 이동 순서

옮기려는 반원의 가장 아래숫자는 마지막(end-3)칸에 바로 옮기고 나머지 반원들은 가운데(mid-2)칸을 거쳐간다. 가장 아래 숫자가 마지막칸으로 바로 가는 이유는 1. 옮길 자리가 두칸밖에 없는데 다른 반원들 위에는 올라갈 수 없고 2. 마지막 맨아래 가야하기 때문에 우선순위가 생긴다. 위에는 이해를 했으나 재귀 호출 순서가 헷갈려서 그려본 결과 아래처럼 호출된다. num=2일때 1번으로 moveTop의 num=0 까지 호출돼서 전부 실행한 경우 2번의 push 로 돌아오고 다시 3번의 끝까지 전부 실행한다. const fs = require("fs"); const n = +fs.readFileSync("/dev/stdin").toString(); const answer = []; function m..

Algorithm/JS 2022.09.08

[알고리즘_Node.js_재귀] 백준 2447번 별 찍기 - 10

n은 3의 거듭제곱이며 가장 작은 별 1개가 모여서 만들어진다. n=3인 경우 n=1 별이 가로 3, 세로 3, 총 9개로 만들어지는데 빈칸의 좌표는 (1,1)이다. n=9인 경우 n=3 별이 가로 3, 세로 3, 총 9개로 만들어지는데 빈칸의 좌표는 (1,1)이다. n=27인 경우 n=9 별이 가로 3, 세로 3, 총 9개로 만들어지는데 빈칸의 좌표는 (1,1)이다. 빈칸의 절대좌표는 (1,1)으로 변함이 없기때문에 이 부분을 빈칸으로 한다. 최소 별 1칸까지 n을 나눈경우 별을 찍어준다. const fs = require("fs"); const n = +fs.readFileSync("/dev/stdin").toString(); let answer = ""; function recursion(x, y,..

Algorithm/JS 2022.09.06

[알고리즘_Node.js_브루트 포스] 백준 2798번 블랙잭

-오름차순으로 정렬한 후 제일 큰 숫자(배열 맨 끝)부터 조합해가며 m과 가까운 수가 나오는걸 찾기 const input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n"); const [n,m] = input[0].split(" ").map(Number); const arr = input[1].split(" ").map(Number); let result = 0; arr.sort((a,b) => a-b); for(let i=n-1; i>=2; i--){ for(let j=i-1; j>=1; j--){ for(let k=j-1; k>=0; k--){ let sum = arr[i] + arr[j] + arr[k]; if(m >=..

Algorithm/JS 2022.08.26

[알고리즘_Node.js_재귀] 백준 17478번 재귀함수가 뭔가요?

const input = require("fs").readFileSync("/dev/stdin").toString(); const n = Number(input); // 최초 1번 나오는 문장 console.log("어느 한 컴퓨터공학과 학생이 유명한 교수님을 찾아가 물었다."); function chatBot(line, n) { // n번만큼 공통 반복 문장 console.log(`${line}"재귀함수가 뭔가요?"`); if (n == 0) {// 재귀 마지막 도달시 나오는 문장 console.log(`${line}"재귀함수는 자기 자신을 호출하는 함수라네"`); console.log(`${line}라고 답변하였지.`);// n+1번 나오기때문에 마지막 도착점에서 한번 출력 return// 재귀로 돌..

Algorithm/JS 2022.08.25

[알고리즘_Node.js_재귀] 백준 10870번 피보나치 수 5

const fs = require("fs"); const input = fs.readFileSync("/dev/stdin").toString(); function fibonacci(input) { if (input == 0){ return 0; } else if(input == 1){ return 1; } for(let i=input; i>=2; i--){ return fibonacci(input-1)+fibonacci(input-2); } } console.log(fibonacci(input)); -다른분들의 풀이방법을 보고 더 간단하게 수정해서 푼 방법. const fs = require("fs"); const input = fs.readFileSync("/dev/stdin").toString(); ..

Algorithm/JS 2022.08.25