백준 1646번/node.js] 평균

김겜김·2023년 11월 17일
0

🎈문제

입력값

출력값


문제해결 방법
Math 객체를 사용하고 Math.max 함수를 이용하여 최댓값을 반환할수 있는 방법을 사용했습니다.
어려웠던점은

(...score)

전개연산자를 사용하는것이었습니다. 그냥 (score) 를 대입하면 값이 NaN이 나오는데
배열을 개별적으로 전달받기위해서 쓰여야하는게 이해가 조금 안갔습니다.
개별연산자에 대해 다시 공부해보고 실전에서 사용할수있게끔 공부해야겠다고 다시한번 생각나게 돠었습니다.


🗂️코드

const fs = require('fs');

//백준제출용
//const input = fs.readFileSync('dev/stdin').toString().split('\n');

//문제 풀이용
const input = fs.readFileSync('example.txt').toString().split('\n');

const num = input[0] * 1;
const score = input[1].split(' ');

const max = Math.max(...score);
let sum = 0;

for (let i = 0; i < num; i++) {
  sum += (score[i] / max) * 100;
}

console.log(sum / num);
profile
개발에 관심이있습니다

0개의 댓글