본문 바로가기
Algorithm

[백준] 단계별로 풀어보기 3단계 | Node.js

by Vintz 2021. 6. 26.
반응형

백준 알고리즘 3단계 for문

01. 구구단

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
});

rl.on('line', (num) => {
  for (let i = 1; i <= 9; i++) {
    console.log(`${num} * ${i} = ${num * i}`);
  }
  rl.close();
}).on('close', () => {
  process.exit();
});

02. A+B - 3

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
});

const input = [];

rl.on('line', (num) => {
  input.push(num);
  if (input.length === Number(input[0]) + 1) rl.close();
}).on('close', () => {
  const cnt = Number(input[0]);
  const nums = [];

  for (let i = 1; i <= cnt; i++) {
    nums.push(input[i].split(' '));
  }
  for (let i = 0; i < cnt; i++) {
    const a = Number(nums[i][0]);
    const b = Number(nums[i][1]);
    console.log(a + b);
  }

  process.exit();
});

03. 합

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
});

rl.on('line', (num) => {
  let sum = 0;

  for (let i = 1; i <= num; i++) {
    sum = i + sum;
  }
  console.log(sum);

  rl.close();
}).on('close', () => {
  process.exit();
});

04. 빠른 A+B

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let answer = '';
rl.on('line', (num) => {
  const input = num.split(' ');

  if (input.length === 2) {
    const a = Number(input[0]);
    const b = Number(input[1]);
    answer += a + b + '\n';
  }
}).on('close', () => {
  console.log(answer);
  process.exit();
});

05. N 찍기

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let answer = '';
rl.on('line', (num) => {
  for (let i = 1; i <= num; i++) {
    answer += i + '\n';
  }
  rl.close();
}).on('close', () => {
  console.log(answer);
  process.exit();
});

06. 기찍 N

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let answer = '';
rl.on('line', (num) => {
  for (let i = num; i >= 1; i--) {
    answer += i + '\n';
  }
  rl.close();
}).on('close', () => {
  console.log(answer);
  process.exit();
});

07. A+B - 7

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let answer = '';
let cnt = 1;
rl.on('line', (num) => {
  const input = num.split(' ');

  if (input.length === 2) {
    const a = Number(input[0]);
    const b = Number(input[1]);
    answer += `Case #${cnt}: ${a + b}\n`;
    cnt += 1;
  }
}).on('close', () => {
  console.log(answer);
  process.exit();
});

08. A+B - 8

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let answer = '';
let cnt = 1;
rl.on('line', (num) => {
  const input = num.split(' ');

  if (input.length === 2) {
    const a = Number(input[0]);
    const b = Number(input[1]);
    answer += `Case #${cnt}: ${a} + ${b} = ${a + b}\n`;
    cnt += 1;
  }
}).on('close', () => {
  console.log(answer);
  process.exit();
});

09. 별 찍기 - 1

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let star = '';
rl.on('line', (num) => {
  for (let i = 1; i <= num; i++) {
    star += '*';
    console.log(star);
  }
  rl.close();
}).on('close', () => {
  process.exit();
});

10. 별 찍기 - 2

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let star = '';
rl.on('line', (num) => {
  for (let i = 0; i < num; i++) {
    for (let j = 0; j < num - i - 1; j++) {
      star += ' ';
    }
    for (let k = 0; k <= i; k++) {
      star += '*';
    }
    star += '\n';
  }
  rl.close();
}).on('close', () => {
  console.log(star);
  process.exit();
});

11. X보다 작은 수

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let answer = '';
let nums = [];
rl.on('line', (num) => {
  nums.push(num.split(' '));
}).on('close', () => {
  const n = Number(nums[0][0]);
  const x = Number(nums[0][1]);
  for (let i = 0; i < n; i++) {
    if (Number(nums[1][i]) < x) answer += `${nums[1][i]} `;
  }
  console.log(nums);
  console.log(answer);
  process.exit();
});

문제들을 풀다보면 함수 내에서의 블록 스코프, 전역 스코프들을 신경써야할 때가 많다. 이번 문제풀이를 통해 블록 범위에 대해 많이 생각하는 계기가 되었다. 별 찍기는 국비지원학원을 다녔을 때 Java로 많이 찍어냈었는데 감회가 새롭다. 이제 백준 사용방법도 많이 익숙해졌고 문제집의 문제들을 하나씩 풀어나가는 기분이다.

반응형