직각삼각형 출력하기

HS K·2023년 3월 9일

문제설명

"*"의 높이와 너비를 1이라고 했을 때, "*"을 이용해 직각 이등변 삼각형을 그리려고합니다. 정수 n 이 주어지면 높이와 너비가 n 인 직각 이등변 삼각형을 출력하도록 코드를 작성해보세요.

제한사항

  • 1 ≤ n ≤ 10

여러종류의 풀이 보기

1번

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

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    solution(Number(input[0]));
});

function solution(n) {
    for(let i = 1; i < n + 1; i++) {
        console.log('*'.repeat(i));
    }
}

2번

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

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    for (let i = 1; i <= +input[0]; i++) {
        console.log('*'.repeat(i));
    }
});

후기

문제를 보자마자 초기화 코드 때문에 당황스러웠던 문제였다.

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

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    console.log(Number(input[0]));
});

문제 조건에 있는 n값도 작성되어있지 않고, 그렇다고 초기화 코드가 있는채로 작성을 해보기도하고, 지우기도 해봤는데 둘다 안되어서 어떻게 시작해야할지 몰랐었다.

하지만 다른사람들의 풀이를 보니 초기화 코드는 잘못 작성되어있는게 아니라, 의도된 코드였다는 것을 알 수 있었다. 따라서 이 문제를 풀기 위해서는 초기화 코드를 이해하고 사용할 줄 알아야 풀 수 있는 문제라는 것을 알 수 있었다.

여태까지 내가 원하는 모양대로 함수만 만들면 되었던 경우와 다르게 주어진 조건(모듈)을 가지고 문제를 풀어야하는 문제도 있다는 것을 알 수 있었다.

profile
주의사항 : 최대한 정확하게 작성하려고 하지만, 틀릴내용이 있을 수도 있으니 유의!

0개의 댓글