알고리즘 : 재귀
이건 내가 푼 문제가 아니다 ㅠㅠ
재귀는 이해는 할 수 있는데 직접 짜려고하면 도저히 이해가 안간다...
const calc = (oper, a, b) => {
if (oper === "+") return a + b;
if (oper === "-") return a - b;
if (oper === "*") return a * b;
};
function solution() {
let input = require("fs")
.readFileSync("input.txt") //"/dev/stdin"
.toString()
.split("\n")
.map((val) => val.trim());
let n = +input.shift();
let arr = input.shift().split("");
// 최대값
let ans = -999999999999;
// 연산자는 +,-,*
// 괄호...흠...?
let num = [];
let oper = [];
for (let i = 0; i < n; i++) {
if (i % 2 === 0) num.push(arr[i]);
else oper.push(arr[i]);
}
num = num.map((v) => +v);
console.log(num, oper);
const check = (here, number) => {
console.log(here, number);
if (here === num.length - 1) {
ans = Math.max(ans, number);
return;
}
check(here + 1, calc(oper[here], number, num[here + 1]));
if (here + 2 <= num.length - 1) {
let temp = calc(oper[here + 1], num[here + 1], num[here + 2]);
check(here + 2, calc(oper[here], number, temp));
}
};
check(0, num[0]);
console.log(ans);
}
solution();
'코딩테스트(알고리즘) > baekjoon' 카테고리의 다른 글
[baekjoon] 16953 A->B (Javascript, Python) (0) | 2023.02.27 |
---|---|
[baekjoon] 12851 숨바꼭질2 (Javascript) (0) | 2023.02.03 |
[baekjoon] 12869 뮤탈리스크 (Javascript) (0) | 2023.02.01 |
[baekjoon] 16234 인구 이동 (Javascript) (0) | 2023.01.31 |
[baekjoon] 2589 보물섬 (Javascript) (0) | 2023.01.30 |