Post

[Programmers] #120823 - 직각삼각형 출력하기 [Java][C++][Python]

[Programmers] #120823 - 직각삼각형 출력하기 [Java][C++][Python]

문제 링크


1. 아이디어

1부터 n까지 순회하면서 i번째 줄에 별을 i개씩 출력하면 높이와 너비가 n인 직각삼각형을 출력할 수 있다.


2. 복잡도

접근시간공간
풀이$O(N^2)$$O(1)$

($N$ = 입력값 n)


3. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.io.*;

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        int n = Integer.parseInt(br.readLine());
        for (int i = 1; i <= n; i++) {
            sb.repeat("*", i).append("\n");
        }

        System.out.println(sb);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++) {
        cout << string(i, '*') << '\n';
    }
}
1
2
3
4
5
6
7
import sys

input = sys.stdin.readline

n = int(input())
for i in range(1, n + 1):
    print("*" * i)

This post is licensed under CC BY 4.0 by the author.