whatisthis?

๋ฐฑ์ค€ 4344 javascript (node.js) ํ’€์ด ๋ณธ๋ฌธ

ALGORITHM/BOJ (Node.js)

๋ฐฑ์ค€ 4344 javascript (node.js) ํ’€์ด

thisisyjin 2022. 1. 4. 13:13

1์ฐจ์› ๋ฐฐ์—ด - (7)

 

 


 

๐Ÿ’ก๋ฌธ์ œ

๋Œ€ํ•™์ƒ ์ƒˆ๋‚ด๊ธฐ๋“ค์˜ 90%๋Š” ์ž์‹ ์ด ๋ฐ˜์—์„œ ํ‰๊ท ์€ ๋„˜๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ๋‹น์‹ ์€ ๊ทธ๋“ค์—๊ฒŒ ์Šฌํ”ˆ ์ง„์‹ค์„ ์•Œ๋ ค์ค˜์•ผ ํ•œ๋‹ค.

๐Ÿ“์ž…๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ํ•™์ƒ์˜ ์ˆ˜ N(1 ≤ N ≤ 1000, N์€ ์ •์ˆ˜)์ด ์ฒซ ์ˆ˜๋กœ ์ฃผ์–ด์ง€๊ณ , ์ด์–ด์„œ N๋ช…์˜ ์ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ ์ˆ˜๋Š” 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜์ด๋‹ค.

๐Ÿ“ˆ์ถœ๋ ฅ

๊ฐ ์ผ€์ด์Šค๋งˆ๋‹ค ํ•œ ์ค„์”ฉ ํ‰๊ท ์„ ๋„˜๋Š” ํ•™์ƒ๋“ค์˜ ๋น„์œจ์„ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ์†Œ์ˆ˜์  ์…‹์งธ ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ

5
5 50 50 70 80 100
7 100 95 90 80 70 60 50
3 70 90 80
3 70 90 81
9 100 99 98 97 96 95 94 93 91

 

์˜ˆ์ œ ์ถœ๋ ฅ

40.000%
57.143%
33.333%
66.667%
55.556%

 

 

 


< ์ฝ”๋“œ >

let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');

let T = Number(input[0]);

for (let i = 1; i <= T; i++) {
  let cases = input[i].split(' ');
  let scoreCount = Number(cases[0]);
  let sum = 0;    // ์ผ€์ด์Šค๋งˆ๋‹ค ์นด์šดํŠธ ์ดˆ๊ธฐํ™”

  for (let j = 1; j <= scoreCount; j++) {
    sum += Number(cases[j]);
  }
  
  let avg = sum / scoreCount;

  let student = 0;
  for (let k = 1; k <= scoreCount; k++) {
    if (cases[k] > avg) {
      student++;
    }
  }
  
  result = (student / scoreCount * 100).toFixed(3);   // ์†Œ์ˆ˜์  ์„ธ์ž๋ฆฌ ๊ณ ์ •
  console.log(result + '%');
}

<KEY POINT>

 

  • input[0]์€ ์ „์ฒด ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๋กœ ์ง€์ •
  • i=1๋ถ€ํ„ฐ i=T๊นŒ์ง€ input[i] ๋กœ for loop (i=0, ์ฆ‰ input[0]๋Š” T์ด๋ฏ€๋กœ 1๋ถ€ํ„ฐ ์‹คํ–‰)
  • ํ‰๊ท ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ํ•ฉ(sum) ๋จผ์ € ๊ตฌํ•จ.
  • avg = sum / ๊ฐฏ์ˆ˜
  • student ๋ณ€์ˆ˜๋กœ ํ‰๊ท ์„ ๋„˜์œผ๋ฉด countํ•ด์คŒ.
  • ๊ฒฐ๊ณผ = ํ‰๊ท ๋„˜์€ํ•™์ƒ์ˆ˜ / ํ•™์ƒ์ˆ˜(=์ ์ˆ˜ ๊ฐฏ์ˆ˜) * 100 
  • ์†Œ์ˆ˜์  ๊ณ ์ •์„ ์œ„ํ•ด toFixed(3) ํ•ด์คŒ

 

+) ํ‰๊ท ์„ ๊ตฌํ•˜๋ ค๋ฉด ? - ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•ด์•ผํ•จ.

for๋ฌธ์„ ์“ฐ๊ธด ํ–ˆ๋Š”๋ฐ ์ง€๋‚œ๋ฒˆ์ฒ˜๋Ÿผ forEach(x => {sum += Number(x)} ) ์ด๋ ‡๊ฒŒ ๋”ํ•ด๋„ ๋จ.

๋‹จ ์—ฌ๊ธฐ์„œ๋Š” , cases[0]์€ ์ ์ˆ˜ ๊ฐœ์ˆ˜์ด๋ฏ€๋กœ ๋นผ์•ผํ•จ. ๊ท€์ฐฎ๋‹ค. ๊ทธ๋ƒฅ ์œ„์—๋Œ€๋กœ ํ•˜์ž! (๋ญ์ง€?)

ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์ƒ๊ฐํ•ด๋‘์ž ใ…ก ์ด๊ฑฐ๋‹ค.

 

 

 

โŒ ํ‹€๋ ธ๋˜ ์ฝ”๋“œ โŒ

let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
let T = input[0] *1 ;
let count = 0;
let percent = 0;

for(let i=1; i<=T; i++) {
   let scoreArr = input[i].split(' ').map(x => Number(x));
   let sum = 0;
   let student = scoreArr[0];

   for(let j=1; j<=student; j++) {
      sum += scoreArr[j];
   }
   let avg = sum / student;

   for(let k=1; k<=student; k++) {
         if(scoreArr[k] > avg) {
            count++;
         }
   }
   
   percent = ((count / student) * 100).toFixed(3);
   console.log(percent + '%');

}