티스토리 뷰
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
71
72
|
import java.io.*;
import java.util.Scanner;
public class boj_2448 {
static Scanner sc = new Scanner(System.in);
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static char[][] map;
static int[] mapColSizes = new int[11];
static int N, k, mapColSize;
public static void main(String[] args) throws IOException {
makeMap();
printMap(0, 0, k);
print();
}
static void makeMap() {
N = sc.nextInt();
int a = N / 3;
while (a > 1) {
k++;
a /= 2;
}
mapColSizes[0] = 5; // k
for (int i = 1; i <= k; i++) {
mapColSizes[i] = (mapColSizes[i-1] * 2) + 1;
}
mapColSize = mapColSizes[k];
map = new char[N][mapColSize];
}
static void printMap(int x, int y, int level) {
// base condition
if (level == 0) {
colorMap(x, y);
return;
}
// recursion
int n = (int) (3 * (Math.pow(2, level)));
printMap(x, y + n/2, level - 1);
printMap(x + n/2, y, level - 1);
printMap(x + n/2, y + mapColSizes[level]/2 + 1, level - 1);
}
static void colorMap(int x, int y) {
map[x][y+2] = '*';
map[x+1][y+1] = '*';
map[x+1][y+3] = '*';
map[x+2][y] = '*';
map[x+2][y+1] = '*';
map[x+2][y+2] = '*';
map[x+2][y+3] = '*';
map[x+2][y+4] = '*';
}
static void print() throws IOException {
for (int i = 0; i < N; i++) {
for (int j = 0; j < mapColSize; j++) {
if (map[i][j] == '*') bw.append('*');
else bw.append(' ');
}
bw.newLine();
}
bw.flush();
bw.close();
}
}
|
cs |
- 재귀
- 귀납적으로 k가 0일때 당연히 그릴 수 있고 k가 n일때는 k가 n-1일때를 이용해서 그릴 수 있으므로 재귀로 풀엇음
- 재귀식을 어떤식으로 해야할지 모르겠어서 좀 오래 걸림
- printMap 은 시작x, 시작y, 현재k를 받아서 맵을 그림
- k가 0일때는 규칙에 맞게 별을 찍어줌
'PS > boj' 카테고리의 다른 글
boj)15665 - N과 M (11) (0) | 2020.11.19 |
---|---|
boj)15664 - N과 M (10) (0) | 2020.11.19 |
boj)15663 - N과 M (9) (0) | 2020.11.18 |
boj)15656 - N과 M (8) (0) | 2020.11.18 |
boj)15656 - N과 M (7) (0) | 2020.11.18 |
링크
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 이펙티브자바 스터디
- 이펙티브자바
- 백준
- 모던자바스크립트
- GCP
- 이펙티브자바 아이템59
- js api
- 김영한 JPA
- HTTP 완벽가이드
- ㅇㄷㅇㅈ
- 킹수빈닷컴
- 프로그래머스
- HTTP 완벽 가이드
- JPA 연관관계 매핑
- 이펙티브자바 아이템60
- 백기선 스터디
- 김영한 http
- js promise
- JS 딥다이브
- 가상 면접 사례로 배우는 대규모 시스템 설계 기초
- http
- 프로그래머스 SQL
- BOJ
- dreamcoding
- Spring Security
- 드림코딩
- REST API
- js array
- 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 | 29 | 30 |
글 보관함