백준 4344 : 평균은 넘겠지 nodejs 풀이

김김김·2022년 3월 28일
0

알고리즘 노트

목록 보기
2/16

문제 링크는 여기로!

다음과 같이 풀었다.

const fs = require("fs");
const input = fs.readFileSync("/dev/stdin").toString().split("\n");

const number = Number(input[0]);

for (let i = 1; i <= number; i++) {
  const arr = input[i].split(" ");
  const length = arr[0];
  let total = 0;
  for (let j = 1; j <= length; j++) {
    total += Number(arr[j]);
  }
  let avg = total / length;
  let overTotal = 0;
  for (let j = 1; j <= length; j++) {
    if (avg < arr[j]) {
      overTotal++;
    }
  }
  console.log(((overTotal / length) * 100).toFixed(3) + "%");
}

앞으론 순수 for문을 애용하기로...

오늘의 수확!
Number.toFixed()를 알게 되었다.
매개변수에 넣은 수만큼 소수점을 남기고 반올림해 주는 메소드.

profile
블로그 이전했습니다. https://sadie100.tistory.com/

0개의 댓글

관련 채용 정보

Powered by GraphCDN, the GraphQL CDN