본문 바로가기
알고리즘 (with. 백준 문제풀이)/Stack

백준 1935번 [후위표기식2] (오답)

by 천릉객 2023. 9. 16.
const fs = require('fs');
const input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const [n, expression, ...num] = input;

// const n = 5;
// const expression = "ABC*+DE/-";
// const num = [1, 2, 3, 4, 5];

const arr = expression.split("");
const stack = [];

for(let p of arr){
    if('A' <= p && p <= 'Z'){
        let index = p.charCodeAt(0) - 'A'.charCodeAt(0);
        stack.push(num[index]);
    }else{
        if(p == '*'){
            let temp = stack.pop()
            stack[stack.length-1] *= temp;
        }else if(p == "/"){
            let temp = stack.pop()
            stack[stack.length-1] /= temp;
        }else if(p == "+"){
            let temp = stack.pop()
            stack[stack.length-1] += temp;
        }else if(p == "-"){
            let temp = stack.pop()
            stack[stack.length-1] -= temp;
        }
    }
}

console.log(stack[0].toFixed(2));

왜 틀렸는지 당최 모르겠다. 5%에서 오답처리 되는데 일단 기록.

'알고리즘 (with. 백준 문제풀이) > Stack' 카테고리의 다른 글

백준 17413번 [단어 뒤집기 2]  (0) 2023.09.16
백준 10799번 [쇠막대기]  (0) 2023.09.15
Stack - 이론  (0) 2023.09.15