๊ด€๋ฆฌ ๋ฉ”๋‰ด

\(@^0^@)/

[JS] ๋ฐฑ์ค€ 4344๋ฒˆ ํ‰๊ท ์€ ๋„˜๊ฒ ์ง€ ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

[JS] ๋ฐฑ์ค€ 4344๋ฒˆ ํ‰๊ท ์€ ๋„˜๊ฒ ์ง€

minjuuu 2021. 9. 28. 20:11
728x90

๐Ÿฑ‍๐Ÿ‘“ 1. ๋ฌธ์ œ : 3052

https://www.acmicpc.net/problem/4344


๐Ÿ”ฅ 2. ํ’€์ด + ์ฝ”๋“œ

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
let input = fs.readFileSync(file).toString().trim().split('\n');
let testCase = Number(input[0]);

// 1. test case์˜ ๊ฐœ์ˆ˜๋ฅผ ์ œ์™ธ ํ•œ ๊ฐ ์ค„์—์„œ ์ธ๋ฑ์Šค 0 ์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ๋”ํ•œ๋‹ค.
// 2. ๊ทธ ๋”ํ•œ ๊ฐ’์„ ๊ทธ ์ค„์˜ ํ•™์ƒ ์ˆ˜๋กœ ๋‚˜๋ˆ„๋ฉด ==> ๊ทธ ์ค„์˜ ํ‰๊ท  ์ ์ˆ˜
// 3. ํ‰๊ท  ์ ์ˆ˜๋ฅผ ๊ทธ ์ค„์˜ ํ•™์ƒ์˜ ์ ์ˆ˜์™€ ๋น„๊ตํ•ด์„œ count ๋˜๋Š” check ๊ฐ™์€ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋ˆ„์ ์‹œํ‚จ๋‹ค.
// 4. ๊ทธ๋ ‡๊ฒŒ ๋ˆ„์ ์‹œํ‚จ ๊ฐ’์„ ๊ฐ ์ค„์˜ ํ•™์ƒ ์ˆ˜ (์ธ๋ฑ์Šค 0)์—์„œ ๋‚˜๋ˆ„๊ณ , ๋น„์œจ์„ ๊ตฌํ•ด์„œ ์…‹์งธ ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅ.
// ex) ํ‰๊ท  ์ ์ˆ˜๋ฅผ ๋„˜์€ ํ•™์ƒ์ด 7๋ช…์ค‘ 4๋ช…์ด๋ผ ํ•˜๋ฉด -> 100/7*4 = 57.143

for (let i = 1; i <= testCase; i++) {
  let sum = 0;
  let count = 0;

  // ์ฒซ์ค„์„ ์ œ์™ธํ•˜๊ณ  ๊ฐ ์ค„์˜ ๊ฐ’์„ ๋ฐฐ์—ด์•ˆ์— ๋‹ด์€ ํ›„, ์ˆซ์ž์—ด๋กœ ๋ณ€ํ™˜
  let test = input[i].split(' ').map(n => Number(n));

  for (let j = 1; j <= test[0]; j++) {
    sum += test[j];
  }

  let avg = sum / test[0];

  for (let k = 1; k <= test[0]; k++) {
    // ๋งŒ์•ฝ์— ๊ฐ๊ฐ์˜ ์ ์ˆ˜๊ฐ€ avg๋ณด๋‹ค ํฌ๋‹ค๋ฉด, count์— ์ถ”๊ฐ€.
    if(test[k] > avg) {
      count++;
    }
  }

  // ํ‰๊ท ๋ณด๋‹ค ํฐ count(์ธ์›)๋ฅผ ๊ฐ ์ค„์˜ ํ•™์ƒ ์ˆ˜๋กœ ๋‚˜๋ˆ„๊ณ , ํผ์„ผํ…Œ์ด์ง€๋ฅผ ๊ตฌํ•ด์„œ
  // ์†Œ์ˆ˜์  ์…‹์งธ ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅ
  console.log((count/test[0]*100).toFixed(3) + "%");
  
}

โšก 3. ๊ทธ ์™ธ

toFixed๋ผ๋Š” ๋ฉ”์„œ๋“œ๋Š” ์ฒ˜์Œ ์•Œ์•˜์Œ! 

โ€ป toFixed ์ฐธ๊ณ  : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

728x90