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

\(@^0^@)/

[JS] ๋ฐฑ์ค€ 1152 ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

[JS] ๋ฐฑ์ค€ 1152 ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜

minjuuu 2021. 10. 16. 19:42
728x90

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

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


๐Ÿ”ฅ 2-1. ์ฝ”๋“œ ๋ฐ ํ’€์ด (์‹คํŒจ)

ใ…‡์ฒ˜์Œ์—๋Š” ๋„ˆ๋ฌด ์‰ฝ๊ฒŒ ์ƒ๊ฐํ•ด์„œ ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜์˜€๋Š”๋ฐ, ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์— ๋ฐ”๋กœ ๋จนํžˆ๊ธธ๋ž˜ ์ œ์ถœํ•˜์˜€๋Š”๋ฐ ๋‹น์—ฐํžˆ ํƒˆ๋ฝ.

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
let input = fs.readFileSync(file).toString().split("\n");
let words = input.join(",").split(" ");

// The Curious Case of Benjamin Button

console.log(words); // [ 'The', 'Curious', 'Case', 'of', 'Benjamin', 'Button' ]
console.log(words.length); // 6


๊ทธ ์ด์œ ๋Š” ์˜ˆ์ œ ์ž…๋ ฅ2์™€ ๊ฐ™์ด ๋ฌธ์žฅ์˜ ์•ž๋ถ€๋ถ„์— ๊ณต๋ฐฑ์ด ์žˆ์„ ๊ฒฝ์šฐ split ํ•˜์˜€์„ ๋•Œ, ๋ฐฐ์—ด ์•ˆ์— ๊ณต๋ฐฑ์ด ๊ฐ™์ด ๋“ค์–ด๊ฐ.
๊ทธ๋ ‡์ง€๋งŒ ๊ณต๋ฐฑ์€ ๋‹จ์–ด๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ, ์ œ์™ธ๋ฅผ ์‹œ์ผœ์•ผ ํ•œ๋‹ค.

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
let input = fs.readFileSync(file).toString().split("\n");
let words = input.join(",").split(" ");

//  The first character is a blank
// ๊ณต๋ฐฑ๋„ ํฌํ•จ๋˜๋ฏ€๋กœ, ์‹คํŒจ
console.log(words); // [ '', 'The', 'first', 'character', 'is', 'a', 'blank' ]
console.log(words.length); // 7

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

trim()์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์žฅ์•ž์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๊ณ , split()์œผ๋กœ ๋‹จ์–ด๋ฅผ ์ธ๋ฑ์Šค์— ๋„ฃ์–ด ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ ,
๊ทธ ๋‹จ์–ด์˜ ์ˆซ์ž๋งŒํผ count๋ฅผ ๋„ฃ์–ด, ๋ช‡ ๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์žˆ๋Š”์ง€ ์ถœ๋ ฅํ•ด๋ƒ„.

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
let input = fs.readFileSync(file).toString();
let words = input.trim().split(" ");
let count = 0;

for (let i = 0; i < words.length; i++) {
  if (words[i] !== '') {
    count++;
  }
}

console.log(count);
728x90