백준 4344번 문제(평균은 넘겠지). JavaScript

코딩아재·2021년 11월 19일
0

코딩 테스트

목록 보기
1/2

안녕하세요!
오늘은 백준 문제 4344번 문제를 리뷰 해보는 시간을 가져보려고 합니다.

문제: 백준 4344번 문제

이번 문제에서 알게된 것은 reduce와 shift 였습니다.

MDN 공식 사이트의 예제

reduce

  • 배열 안에 있는 각각의 값을 더해주는 것을 활용했습니다.
const array1 = [1, 2, 3, 4];
const reducer = (previousValue, currentValue) => previousValue + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15

shift

  • 배열의 첫 번째 요소를 제거하고, 제거된 요소를 반환하는 것을 이용했습니다.
const array1 = [1, 2, 3];

const firstElement = array1.shift();

console.log(array1);
// expected output: Array [2, 3]

console.log(firstElement);
// expected output: 1

풀이

const input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
let num = parseInt(input[0]);

for(let i =1; i<=num; i++){
  let newNum = input[i].split(' ').map(a => parseInt(a));
  let newCo = parseInt(newNum.shift());
  let sum = 0;
  let avg = 0;
  sum = newNum.reduce((a, b) => a+b);
  avg = sum / newCo;
  let count = newNum.filter(a => a > avg).length;
  console.log((count/newNum.length*100).toFixed(3)+"%")
}
profile
코딩하는 아재입니다.

0개의 댓글

관련 채용 정보