Updated:

1. 문제 링크

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

2. 사용 알고리즘

구현

3. 풀이

반복문을 돌며 별 출력

4. 소스 코드

4-1. C++

https://github.com/dev-aiden/problem-solving/blob/main/boj/10992.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main(void) {
    ios_base::sync_with_stdio(false);
    int n; cin >> n;
    if (n == 1) {
        cout << "*\n";
        return 0;
    }
    for (int i = 0; i < n - 1; ++i) {
        for (int j = i; j < n - 1; ++j) cout << " ";
        cout << "*";
        if (i != 0) {
            for (int j = 0; j < i * 2 - 1; ++j) cout << " ";
            cout << "*";
        }
        cout << "\n";
    }
    for (int j = 0; j < n * 2 - 1; ++j) cout << "*";
    cout << "\n";
    return 0;
}

4-2. JAVA

https://github.com/dev-aiden/problem-solving/blob/main/boj/10992.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    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());
        if(n == 1) {
            System.out.println("*");
            return;
        }
        for (int i = 0; i < n - 1; ++i) {
            for (int j = i; j < n - 1; ++j) sb.append(" ");
            sb.append("*");
            if (i != 0) {
                for (int j = 0; j < i * 2 - 1; ++j) sb.append(" ");
                sb.append("*");
            }
            sb.append("\n");
        }
        for (int j = 0; j < n * 2 - 1; ++j) sb.append("*");
        sb.append("\n");
        System.out.println(sb);
    }
}

Updated:

Leave a comment