직각삼각형 출력하기

반즈·2023년 12월 8일

프로그래머스 입문

목록 보기
37/51

문제 설명

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

입출력 예


자바

나의 풀이

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String star = "";
        for(int i = 1; i <= n; i++){
            star += "*";
            System.out.println(star);
        }
    }
}

참고 풀이 1 (.repeat())

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        for(int i=1; i<=n; i++){
            System.out.println("*".repeat(i));
        }
    }
}

참고 풀이 2 (이중 for문)

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        for(int i=0; i<n; i++){
            for(int j=0; j<=i; j++){
                System.out.print("*");
            }
            System.out.println();
        }

    }
}

자바스크립트

나의 풀이

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));   
    }
});
profile
나를 채우다

0개의 댓글