250x250
Notice
Recent Posts
Recent Comments
Link
\(@^0^@)/
[JS] ๋ฐฑ์ค 1152 ๋จ์ด์ ๊ฐ์ ๋ณธ๋ฌธ
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
'์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๊ณ ๋ฆฌ์ฆ] ์ฐ๊ฒฐ ๋ฆฌ์คํธ(LinkedList)์ ๊ตฌํ ๋ฉ์๋ (0) | 2022.05.03 |
---|---|
Big O Notation (0) | 2021.12.14 |
[JS] ๋ฐฑ์ค 1157 ๋จ์ด๊ณต๋ถ (0) | 2021.10.16 |
[JS] ๋ฐฑ์ค 2675 ๋ฌธ์์ด ๋ฐ๋ณต (0) | 2021.10.11 |
[JS] ๋ฐฑ์ค 10809 ์ํ๋ฒณ ์ฐพ๊ธฐ (0) | 2021.10.11 |