본문 바로가기
개발 노트/백준, 프로그래머스 풀이

[백준 9733/javascript] 꿀벌

by tokkiC 2022. 9. 23.

객체로 카운트 하는 간단한 문제

근데! 왜! 뭐가 틀린지 모르겠는 문제!

출력 형식도 맞는데 100%에서 실패가 뜬다

다른 언어랑 로직도 별 차이 없는데 왜... 혹시나해서

toFixed(2) 대신에 100을 곱하고 Math.floor()로 내림 후 100으로 나눠도 오답으로 뜬다

문제 이상해... 뭐가 틀린건데... 

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

 

9733번: 꿀벌

각각의 일을 한 횟수와 비율을 공백으로 구분하여 출력한다. 출력은 {Re,Pt,Cc,Ea,Tb,Cm,Ex} 순서대로 하며, 비율은 소수점 둘째 자리까지 출력한다. 주어진 목록에 없는 일은 출력하지 않는다. 입력의

www.acmicpc.net

let input = [];

const readline = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

readline.on("line", (line) => {
  input.push(line);
});

readline.on("close", () => {
  solution(input);
  process.exit();
});

const solution = (inp) => {
  let arr = [];
  for (el of inp) {
    arr.push(...el.split(" "));
  }
  let oj = {
    Re: 0,
    Pt: 0,
    Cc: 0,
    Ea: 0,
    Tb: 0,
    Cm: 0,
    Ex: 0,
  };
  for (el of arr) {
    if (oj[el] !== undefined) {
      oj[el]++;
    }
  }
  let ans = [];
  let n = 1 / arr.length;
  for (el in oj) {
    ans.push(`${el} ${oj[el]} ${(n * Number(oj[el])).toFixed(2)}`);
  }
  ans.push(`Total ${arr.length} 1.00`);
  console.log(ans.join("\n"));
};

댓글