언젠가는 펼쳐 볼 아카이브

[BOJ] 5073번 - 삼각형과 세 변 본문

IT/Baekjoon Oline Judge

[BOJ] 5073번 - 삼각형과 세 변

개발자희망생고롸파덕 2023. 9. 11. 19:17

사용언어 : javascript - node.js

 

#첫번째 제출코드

const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
const input = fs
  .readFileSync(filePath)
  .toString()
  .trim()
  .split('\n')
  .map((item) =>
    item
      .trim()
      .split(' ')
      .map((item) => +item)
  );

input.forEach((item) => {
  if (item[0] !== 0 && item[2] !== 0) {
    solution(item);
  }
});

function solution(item) {
  item.sort((a, b) => a - b);
  const a = item[0];
  const b = item[1];
  const c = item[2];

  if (c >= a + b) {
    console.log('Invalid');
  } else if (a === b && b === c) {
    console.log('Equilateral');
  } else if (a === b || b === c) {
    console.log('Isosceles');
  } else {
    console.log('Scalene');
  }
}

>> 결과 : 런타임 에러(NZEC)

 

이이잉...?? vs code에서 테스트 한 결과 값들은 다 맞았는데 런타임 에러가 떴다.

백준 온라인에서는 제출하다보면 가끔 오류가 나는데.. 런타임 에러 원인을 찾아보니, "NZEC(None Zero Exit Code) 비 제로 종료 코드" 라고 한다. 

음.. 위에 내 코드를 다시 보니 return; 으로 마지막으로 종료해주는 부분이 없어서 생긴 에러 같다. 그렇다면 solution 호출이 종료 되는 시점에서 'return;'을 해주고, solution 함수도 return 값이 있도록 수정해주었다.

 

#최종제출 코드

const fs = require('fs');
const { it } = require('node:test');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
const input = fs
  .readFileSync(filePath)
  .toString()
  .trim()
  .split('\n')
  .map((item) =>
    item
      .trim()
      .split(' ')
      .map((item) => +item)
  );

input.forEach((item) => {
  if (item[0] === 0 && item[2] === 0) {
    return;
  } else {
    console.log(solution(item));
  }
});

function solution(item) {
  let answer = '';
  item.sort((a, b) => a - b);
  const a = item[0];
  const b = item[1];
  const c = item[2];

  if (c >= a + b) {
    answer = 'Invalid';
  } else if (a === b && b === c) {
    answer = 'Equilateral';
  } else if (a === b || b === c) {
    answer = 'Isosceles';
  } else {
    answer = 'Scalene';
  }

  return answer;
}

>> 결과 : 맞았습니다!