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

\(@^0^@)/

[JS] ๋ฐฑ์ค€ 2577๋ฒˆ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜ ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

[JS] ๋ฐฑ์ค€ 2577๋ฒˆ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜

minjuuu 2021. 9. 28. 01:47
728x90

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

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


๐Ÿ”ฅ 2.1 split์„ ์ด์šฉํ•œ ์ฝ”๋“œ

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt';

// Num์œผ๋กœ ๋ฐ”๊ฟ”์•ผ ๊ณต๋ฐฑ์—†์ด ์ œ๋Œ€๋กœ ๋น„๊ต ๊ฐ€๋Šฅ.
let input = fs.readFileSync(file).toString().split('\n').map(n => Number(n));

let first = input[0];
let sec = input[1];
let third = input[2];
let multi = String(first * sec * third);

// ๊ฐ„๊ฒฐํ•˜๊ฒŒ let multi = String(input[0] * input[1] * input[2]) ์ด๋ ‡๊ฒŒ ํ•  ์ˆ˜๋„ ์žˆ์Œ!

for (let i = 0; i <= 9; i++) { // i < 10์„ ์“ฐ๊ธฐ๋„ ํ•จ. (2.2 ์ฐธ๊ณ )
  // console.log(multi.split(i + '').length) ์ด๋ ‡๊ฒŒ๋งŒ ํ•œ๋‹ค๋ฉด,
  // 1-9๊นŒ์ง€๊ฐ€ ์•„๋‹ˆ๋ผ, 0-9๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ๊ตฌํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ, -1์„ ํ•ด์ค€๋‹ค.

  console.log(multi.split(i + '').length -1)
}

๐Ÿ”ฅ 2.2 ์ค‘์ฒฉ for๋ฌธ์„ ์ด์šฉํ•œ ์ฝ”๋“œ

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
// Num์œผ๋กœ ๋ฐ”๊ฟ”์•ผ ๊ณต๋ฐฑ์—†์ด ์ œ๋Œ€๋กœ ๋น„๊ต ๊ฐ€๋Šฅ.
let input = fs.readFileSync(file).toString().split('\n').map(n => Number(n));

let multi = String(input[0] * input[1] * input[2]);

// for๋ฌธ์—์„œ ๊ตฌํ•œ multi[]๊ฐ’์ด i์™€ ์ผ์น˜ํ•˜๋‹ค๋ฉด count๊ฐ’์„ ์˜ฌ๋ ค์คŒ.
let count;

for (let i = 0; i <= 9; i++) {
  count = 0;
  for(let j = 0; j < multi.length; j++) {
    if(multi[j] == i) {
    // Number(multi[j] === i) ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Œ.
      count++;
    }
  }
  console.log(count);
}

 

728x90