문제 해설

 

 현재 높이의 2배의 삼각형이 다음에 만들어진다는 것에서 착안하여 문제를 풀었습니다.

 

 

 즉, 위의 그림과 같은 규칙으로 삼각형이 생성됩니다. 따라서 이를 코드로 구현하면 아래와 같습니다.

(+ null 문자열이 출력되지 않도록 유의합니다.)

 

 

코드

 

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
 
using namespace std;
 
char d[3072][6144];
 
void star(int n) {
 
    for (int i = n / 2; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            d[i][j] = d[i - n / 2][j];
    }
 
    for (int i = n / 2; i < n; i++)
    {
        for (int j = n; j < 2 * n; j++)
            d[i][j] = d[i][j - n];
    }
 
    for (int i = 0; i < n / 2; i++)
    {
        for (int j = (2 * n) - (n / 2- 1; j >= n / 2; j--)
        {
            d[i][j] = d[i][j - (n / 2)];
            d[i][j - (n / 2)] = ' ';
        }
    }
    return;
}
 
int main() {
 
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    int n;
    cin >> n;
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 2 * n; j++)
            d[i][j] = ' ';
    }
 
    //initial triangle
    d[0][2= '*';
    d[1][1= '*';
    d[1][3= '*';
    for (int i = 0; i < 5; i++)
        d[2][i] = '*';
 
    for (int i = 3; i <= n; i *= 2)
    {
        if (i == 3)
            continue;
        else
            star(i);
    }
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 2 * n; j++)
            cout << d[i][j];
        cout << '\n';
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

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

 

2448번: 별 찍기 - 11

첫째 줄에 N이 주어진다. N은 항상 3×2k 수이다. (3, 6, 12, 24, 48, ...) (k ≤ 10)

www.acmicpc.net

 

+ Recent posts