티스토리 뷰

https://www.acmicpc.net/problem/3425

 

3425번: 고스택

각각의 입력값에 대해서, 해당하는 프로그램을 수행한 뒤, 출력값을 출력하면 된다. 출력값이란 스택에 저장되어 있는 숫자이다. 만약, 프로그램 에러가 발생하거나, 모든 수행이 종료됐을 때

www.acmicpc.net

 

 

##반례##

입력)

ADD
END
3
0
42
3

QUIT

 

출력)

ERROR
ERROR
ERROR

 

 

class Node {
  constructor(value) {
    this.value = BigInt(value);
    this.prev = null;
  }
}

class Gostack {
  isError = false;
  constructor(first) {
    const firstNode = new Node(first);
    this.top = firstNode;
    this.length = 1;
  }

  num(x) {
    if (this.isError) {
      return;
    }
    if (BigInt(x) > BigInt(1000000000) || BigInt(x) < BigInt(-1000000000)) {
      this.findError();
      return;
    }
    const node = new Node(x);
    node.prev = this.top;
    this.top = node;
    this.length++;
  }

  pop() {
    if (this.isError) {
      return;
    }
    if (this.length == 0) {
      this.findError();
      return;
    }
    const item = this.top;
    this.top = this.top.prev;
    this.length--;
    return item.value;
  }

  inv() {
    if (this.isError) {
      return;
    }
    this.top.value = BigInt(-1) * this.top.value;
  }

  dup() {
    if (this.isError) {
      return;
    }
    const value = this.top.value;
    this.num(value);
  }

  swp() {
    if (this.isError) {
      return;
    }
    const first = this.top;
    const second = this.top.prev;
    first.prev = second.prev;
    second.prev = first;
    this.top = second;
  }

  add() {
    if (this.isError) {
      return;
    }

    if (this.length < 2) {
      this.findError();
      return;
    }
    const first = this.pop();
    const second = this.pop();
    this.num(first + second);
  }

  sub() {
    if (this.isError) {
      return;
    }
    if (this.length < 2) {
      this.findError();
      return;
    }
    const first = this.pop();
    const second = this.pop();
    this.num(second - first);
  }

  mul() {
    if (this.isError) {
      return;
    }
    if (this.length < 2) {
      this.findError();
      return;
    }
    const first = this.pop();
    const second = this.pop();
    this.num(first * second);
  }

  div() {
    if (this.isError) {
      return;
    }
    if (this.length < 2) {
      return;
    }
    const first = this.pop();
    const second = this.pop();
    if (first == BigInt(0)) {
      this.findError();
      return;
    }
    this.num(second / first);
  }

  mod() {
    if (this.isError) {
      return;
    }
    const first = this.pop();
    const second = this.pop();
    if (first == BigInt(0)) {
      this.findError();
      return;
    }

    let modResult = second - (second / first) * first;
    if (second * modResult < 0) modResult *= BigInt(-1);
    this.num(modResult);
  }

  end() {
    if (this.isError || this.length != 1) {
      return "ERROR";
    } else {
      return Number(this.top.value);
    }
  }

  findError() {
    this.isError = true;
  }
}

const fs = require("fs");
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n");
const inputArr = [[]];
let location = 0;
input.forEach((v) => {
  if (v == "") {
    inputArr.push([]);
    location++;
  } else inputArr[location].push(v);
});

inputArr.pop();

for (let k = 0; k < inputArr.length; k++) {
  const machine = inputArr[k];
  const program = [];
  const inputArea = [];

  machine.forEach((command) => {
    if (isNaN(Number(command))) {
      program.push(command);
    } else {
      inputArea.push(command);
    }
  });

  const N = inputArea.shift();

  for (let i = 0; i < N; i++) {
    const gostack = new Gostack(+inputArea[i]);
    program.forEach((v) => {
      const c = v.substring(0, 3);
      switch (c) {
        case "NUM":
          const [_, x] = v.split(" ");
          gostack.num(BigInt(x));
          break;
        case "POP":
          gostack.pop();
          break;
        case "INV":
          gostack.inv();
          break;
        case "DUP":
          gostack.dup();
          break;
        case "SWP":
          gostack.swp();
          break;
        case "ADD":
          gostack.add();
          break;
        case "MUL":
          gostack.mul();
          break;
        case "SUB":
          gostack.sub();
          break;
        case "DIV":
          gostack.div();
          break;
        case "MOD":
          gostack.mod();
          break;
        case "END":
          const end = gostack.end();
          console.log(end);
          break;
      }
    });
  }

  if (k != inputArr.length - 1) {
    console.log("");
  }
}
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함