티스토리 뷰

PS/boj

boj)16505 - 별

kingsubin 2020. 11. 14. 11:27
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
import java.io.*;
import java.util.Scanner;
 
public class boj_16505 {
    static char[][] map;
    static int N, length;
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        length = (int)Math.pow(2, N);
 
        map = new char[length][length];
 
        func(00length);
 
        // print
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                if (j == length - i) break;
                if (map[i][j] == '*') bw.write(map[i][j]);
                else bw.write(' ');
            }
            bw.newLine();
        }
 
        bw.flush();
        bw.close();
    }
 
    private static void func(int x, int y, int length) {
        // base condition
        if (length == 1) {
            map[x][y] = '*';
            return;
        }
 
        // recursion
        int newLength = length/2;
        func(x, y, newLength);
        func(x, y + newLength, newLength);
        func(x + newLength, y, newLength);
    }
 
}
 
cs

 

- 재귀

 

- 재귀랑 좀 친해진거 같다.

 

 

 


www.acmicpc.net/problem/16505

'PS > boj' 카테고리의 다른 글

boj)5904 - Moo 게임  (0) 2020.11.15
boj)2447 - 별 찍기 - 10  (0) 2020.11.14
boj)1992 - 쿼드트리  (0) 2020.11.13
boj)1780 - 종이의 개수  (0) 2020.11.13
boj)17478 - 재귀함수가 뭔가요?  (0) 2020.11.12