문제출처: https://www.acmicpc.net/problem/2448
참고 출처: http://developer-syubrofo.tistory.com/33
Fractal 형태의 문제. Fractal은 작은 구조가 전체 구조와 비슷한 형태로 끝없이 되풀이 되는 구조를 말한다.
(그림출처: http://developer-syubrofo.tistory.com/33)
N=3, 6, 12일 때 프랙탈 삼각형을 보여준다.
1. N=3일 때의 삼각형의 string을 초기화한다.
Star[0] = " * "
Star[1] = " * * "
Star[2] = "*****"
2-1. N=6일 때, 4~6줄은 위의 문자열(Star[0], Star[1], Star[2])을 사용하여 만든다.
Star[3] = Star[0] + " " + Star[0];
Star[4] = Star[1] + " " + Star[1];
Star[5] = Star[2] + " " + Star[2];
(그림출처: http://developer-syubrofo.tistory.com/33)
2-2. 나머지 1~3줄은 양 옆에 공백 3칸을 더해 맞춘다.
Star[0] = " " + Star[0] + " ";
Star[1] = " " + Star[1] + " ";
Star[2] = " " + Star[2] + " ";
(그림출처: http://developer-syubrofo.tistory.com/33)
2-3. N=12일 때의 삼각형을 위해 기존의 양옆 공백의 길이를 3칸을 더해 6칸을 만든다.
양옆 공백의 길이가 N/2이다. N=6이라면 공백은 3칸, N=12라면 공백은 6칸이다.
2번 과정을 반복한다.
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 | import java.io.BufferedReader; import java.io.InputStreamReader; // 참고출처: http://developer-syubrofo.tistory.com/33 public class Main { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(reader.readLine()); String[] star = new String[N]; star[0] = " * "; star[1] = " * * "; star[2] = "*****"; String space = " "; // 공백 3칸 for(int i=3, k=1 ; i<N ; i<<=1, k<<=1){ // i와 k가 2배씩 증가 for(int j=0 ; j<i ; j++) { star[i+j] = star[j] + " " + star[j]; // 4~6줄 star[j] = space + star[j] + space; // 1~3줄 } for(int j=0 ; j<k ; j++) space += " "; // 공백 3칸 } StringBuilder builder = new StringBuilder(); for(int i=0 ; i<star.length ; i++) builder.append(star[i]).append("\n"); System.out.println(builder); } } | cs |
'Algorithm' 카테고리의 다른 글
백준 2577번: 숫자의 개수 (0) | 2018.10.15 |
---|---|
백준 1152번: 단어의 개수 (0) | 2018.10.15 |
백준 1065번: 한수 (0) | 2018.10.04 |
백준 4673번: 셀프 넘버 (0) | 2018.10.04 |
백준 2839번: 설탕 배달 (0) | 2018.09.30 |