const fs = require('fs');
const path = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
const input = fs.readFileSync(path).toString().trim().split('\n');
function solve(input) {
const caseCount = Number(input[0]);
for (let i = 1; i <= caseCount; i++) {
let arrTemp = input[i].split(' ').map((x) => Number(x));
let studentCount = arrTemp.shift();
let avgScore = arrTemp.reduce((acc, cur) => acc + cur) / studentCount;
let overAvg = arrTemp.filter((score) => score > avgScore);
let answer =
Math.round((overAvg.length / arrTemp.length) * 100 * 1000) / 1000;
console.log(`${answer.toFixed(3)}%`);
}
}
solve(input);
reduce ๋ฐ filter์ ๋ํ ํ์ฉ์ด ์ด๋์ ๋ ์ต์ํด์ ธ๊ฐ๋ ๊ฒ ๊ฐ๋ค.
๋, ๋ฐฐ์ด์ ์ค๋ฅธ์ชฝ์์ ๋นผ๋ pop๊ณผ ๋ค๋ฅด๊ฒ ์ผ์ชฝ๋ถํฐ ๋นผ๋ shift๋ฅผ ๋ค์ ํ๋ฒ ๊ธฐ์ตํ ์ ์์๋ค.
์์ ๋ด์ฉ๋ค๋ ๋ณต์ต์ด ๋์์ง๋ง, ๊ฐ์ธ์ ์ผ๋ก ์ด ๋ฌธ์ ์์ ๊ฐ์ฅ ํฐ ๋ฐฐ์์ JS์ ์์ ํ๊ธฐ๋ฒ์ด๋ค.
ํ๊ท ์ด ๋๋ ํ์๋ค์ ๋น์จ์ ์ํด *100 ์ ํด์ฃผ๊ณ , ์์์ ์๋ 3์๋ฆฌ๊น์ง ํํํ๊ธฐ ์ํด *1000์ ํด์
์ ์๊ฐ์ ๋ํด ๋ฐ์ฌ๋ฆผ์ ํด์ฃผ๊ณ ๋ค์ 1000์ผ๋ก ๋๋ ์ฃผ์๋ค.
๊ทผ๋ฐ ์ด๋ ๊ฒ๋ง ํ๋ฉด ๋ฌธ์ ์ ์ด ๋ฑ ๋๋์ด๋จ์ด์ง๋ ๊ฒฝ์ฐ์ ์ด๋ ๊ฒ ํด๋ ์ ์๋ก ๊ฐ์ด ๋์์ ๋ค์ 000์ด ๋ถ์ง ์๋๋ค.
์ด๋ด ๋ ์ธ ์ ์๋ ๋ฐฉ๋ฒ์ด float ๊ฐ์ฒด์์ ์ฌ์ฉ๊ฐ๋ฅํ toFixed์ด๋ค!
toFix(n) => ์์์ ์๋ n์ ์๋ฆฌ๊น์ง ํํ(์๋ค๋ฉด 0์ผ๋ก ์ฑ์์ ํํ)
'๐ JavaScript > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS] 3052 (0) | 2022.05.14 |
---|---|
[JS] 8958 (0) | 2022.05.14 |
[JS] 1110 (0) | 2022.05.12 |
[JS] 10951 (0) | 2022.05.12 |
[JS] 10871 (0) | 2022.05.11 |