안녕하세요!
오늘은 백준 문제 4344번 문제를 리뷰 해보는 시간을 가져보려고 합니다.
이번 문제에서 알게된 것은 reduce와 shift 였습니다.
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
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)+"%")
}