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

\(@^0^@)/

[JS] ๋ฐฑ์ค€ 3052๋ฒˆ ๋‚˜๋จธ์ง€ ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

[JS] ๋ฐฑ์ค€ 3052๋ฒˆ ๋‚˜๋จธ์ง€

minjuuu 2021. 9. 28. 02:34
728x90

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

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


๐Ÿ”ฅ 2.1 Set์„ ์ด์šฉํ•œ ํ’€์ด

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
// ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž๋ฐฐ์—ด์„ ์ˆซ์ž๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜, Num์œผ๋กœ ๋ฐ”๊ฟ”์•ผ ๊ณต๋ฐฑ์—†์ด ์ œ๋Œ€๋กœ ๋น„๊ต ๊ฐ€๋Šฅ.
// trim()์„ ์ ์šฉํ•ด์•ผ๋งŒ ๋งˆ์ง€๋ง‰์— ๊ณต๋ฐฑ ''๊ฐ€ ์—†์Œ.
let input = fs.readFileSync(file).toString().trim().split('\n').map(n => Number(n));

//[ 39, 40, 41, 0, 1, 2, 40, 41, 0, 1]
let arr = input.map((n) => n % 42);

// set ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ค‘๋ณต๋œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐ.
let set = new Set(arr);

// ์ค‘๋ณต๋˜์ง€ ์•Š์€ ์š”์†Œ๋“ค๋งŒ ๋”ฐ๋กœ ๋ฐฐ์—ด ์ƒ์„ฑ.
let NotSameNum = [...set];

console.log(NotSameNum.length);

 

๐Ÿ”ฅ 2.1 for๋ฌธ์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
// ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž๋ฐฐ์—ด์„ ์ˆซ์ž๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜, Num์œผ๋กœ ๋ฐ”๊ฟ”์•ผ ๊ณต๋ฐฑ์—†์ด ์ œ๋Œ€๋กœ ๋น„๊ต ๊ฐ€๋Šฅ.
// trim()์„ ์ ์šฉํ•ด์•ผ๋งŒ ๋งˆ์ง€๋ง‰์— ๊ณต๋ฐฑ ''๊ฐ€ ์—†์Œ.
let input = fs.readFileSync(file).toString().trim().split('\n');

let check = [];
let num;

for (let i = 0; i < input.length; i++) {
  num = input[i] % 42;
  
  // ๋งŒ์•ฝ check ์ธ๋ฑ์Šค ์š”์†Œ์•ˆ์— num์ด ์—†์„ ๊ฒฝ์šฐ, ๋ฐฐ์—ด์•ˆ์— ํ•ด๋‹น ๊ฐ’์„ push
  if (check.indexOf(num) === -1) check.push(num);
}
console.log(check.length);
728x90