๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[๋ฐฑ์ค€/Node.js] 4344๋ฒˆ ํ‰๊ท ์€ ๋„˜๊ฒ ์ง€ || ํ’€์ด JavaScript

728x90

 

<๋งž์•˜์Šต๋‹ˆ๋‹ค>

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

for (let i = 1; i <= test; i++) {
    let sub = input[i].split(" ").map(v => Number(v));
    let student = sub.shift();
    let ever = sub.reduce((sum, v) => sum + v, 0) / student;
    let overEver = sub.filter(v => v > ever);
    let percent = ((overEver.length / student )* 100).toFixed(3);
    console.log(percent + '%');
}

test ํ…Œ์ŠคํŠธ ์ˆ˜๋ฅผ ๋ฐ›์•„์™€์„œ for ๋ฌธ์œผ๋กœ ๊ฐ’์„ ๋„์ถœํ•œ๋‹ค.

๊ณผ๋ชฉ๊ณผ ํ•™์ƒ ์ˆ˜๊ฐ€ ํฌํ•จ๋œ ๋ฐฐ์—ด์„ sub ๋กœ ๋ฐ›์•„์˜จ ํ›„, shift() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ•™์ƒ ์ˆ˜๋งŒ student๋กœ ๋–ผ์–ด์ค€๋‹ค.

shift() ํ•จ์ˆ˜๋Š” ๋–ผ์–ด๋‚ธ ๋งจ ์•ž ๊ฐ’์„ return ํ•œ๋‹ค.

์ ์ˆ˜๋งŒ ๋‚จ์€ sub ๋ฐฐ์—ด์„ reduce ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋‹ค ๋”ํ•ด์ค€ ๋’ค, ํ•™์ƒ์ˆ˜๋กœ ๋‚˜๋ˆ  ํ‰๊ท ์„ ๊ตฌํ•ด์ค€๋‹ค.

ํ‰๊ท ๋ณด๋‹ค ํฐ ์ ์ˆ˜๋“ค๋งŒ overEver ์— ๋„ฃ์–ด์ค€๋‹ค. 

overEver์˜ ๊ธธ์ด (=ํ‰๊ท ๋ณด๋‹ค ํฐ ์ ์ˆ˜๋“ค ๊ฐœ์ˆ˜) ๋ฅผ ํ•™์ƒ ์ˆ˜๋กœ ๋‚˜๋ˆ ์ค€ ํ›„ 100 ์„ ๊ณฑํ•˜๋ฉด ๋น„์œจ์ด ๋‚˜์˜ค๋Š”๋ฐ, 

์†Œ์ˆ˜์  ์„ธ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅํ•ด์•ผํ•˜๋‹ˆ toFixed() ๋กœ ์†Œ์ˆ˜์ ์„ ๊ณ ์ •ํ•ด์ฃผ๊ณ  '%' ๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค.

 

 

<ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค>

const input = require('fs').readFileSync('/dev/stdin').toString().trim().split("\r");
const test = input[0]; //5

const everFn = (sub) => {
    let student = sub.shift();
    let ever = sub.reduce((sum, v) => sum + v) / student;
    let overEver = sub.filter(v => v > ever);
    let percent = (overEver.length / student * 100).toFixed(3);
    console.log(percent + '%');
}

for (let i = 1; i <= test; i++) {
    let sub = input[i].split(" ").map(v => Number(v));
    everFn(sub);
}

์ฒ˜์Œ์—” ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์„œ ์ œ์ถœํ–ˆ๋Š”๋ฐ, ๊ณ„์† ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค๊ฐ€ ๋‚˜์™”๋‹ค. ์™œ์ธ์ง€ ๋ชป ์•Œ์•„๋ƒˆ๋‹คใ… 

728x90
๋ฐ˜์‘ํ˜•