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

\(@^0^@)/

[JS] ๋ฐฑ์ค€ 2439๋ฒˆ ๋ณ„ ์ฐ๊ธฐ - 2 ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

[JS] ๋ฐฑ์ค€ 2439๋ฒˆ ๋ณ„ ์ฐ๊ธฐ - 2

minjuuu 2021. 9. 17. 17:43
728x90

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

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


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

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

let result = '';
let blank = '';
for (let i = 1; i <= input[0]; i++){
  result += "*";
  for (let j = 0; j < input[0]-i; j++){
    blank += ' ';
  }
  console.log(blank + result);
  blank = '';
}

๐Ÿ”ฅ 2.2 join ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ

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

let num = Number(input);

// new Array(num) ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด์„œ fill ๋ฉ”์„œ๋“œ๋กœ ๊ณต๋ฐฑ์„ ์ฑ„์šด๋‹ค.
// [ ' ', ' ', ' ', ' ', ' ' ]
let star = new Array(num).fill(' ');

// ์ธ๋ฑ์Šค์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’(์—ฌ๊ธฐ์„  ์ธ๋ฑ์Šค4)๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ star ๋ฐฐ์—ด์— ๋„ฃ๋Š”๋‹ค.
for (let i = num - 1; i >= 0; i--){
  star[i] = "*";
console.log(star.join(''));
}

๐Ÿ”ฅ 2.3 repeat ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ

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


for (let i = 1; i <= input[0]; i++){
  console.log(" ".repeat(input[0] - i) + "*".repeat(i));
}

โšก ํ™•์‹คํžˆ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‹œ๊ฐ„๋„ ๋‹จ์ถ•๋˜๊ณ , ๋ฉ”๋ชจ๋ฆฌ๋„ ์ค„์–ด๋“ ๋‹ค
๋‚˜๋„ ๋น ๋ฅธ ์‹œ์ผ ๋‚ด์— ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ’€ ๋•Œ, ๋ฉ”์„œ๋“œ๋“ค์„ ์ ์žฌ์ ์†Œ์— ๋„ฃ๊ณ  ์‹ถ๋‹ค~~


โ€ป ์ฐธ๊ณ  : https://gurtn.tistory.com/33

โ€ป ์ฐธ๊ณ  : https://ryulurala.tistory.com/249

728x90