알고리즘 |
재귀, 분할정복 |
[문제 본문]
더보기
문제
예제를 보고 규칙을 유추한 뒤에 별을 찍어 보세요.
입력
첫째 줄에 N이 주어진다. N은 항상 3×2k 수이다. (3, 6, 12, 24, 48, ...) (0 ≤ k ≤ 10, k는 정수)
출력
첫째 줄부터 N번째 줄까지 별을 출력한다.
예제 입력 1
24
예제 출력 1
* * * ***** * * * * * * ***** ***** * * * * * * ***** ***** * * * * * * * * * * * * ***** ***** ***** ***** * * * * * * ***** ***** * * * * * * * * * * * * ***** ***** ***** ***** * * * * * * * * * * * * ***** ***** ***** ***** * * * * * * * * * * * * * * * * * * * * * * * * ***** ***** ***** ***** ***** ***** ***** *****
[푼 코드]
#include <iostream>
using namespace std;
#define MAXNUM 3072
char arr[MAXNUM][MAXNUM * 2];
int N;
void draw(int x, int y, int n)
{
if (n == 3)
{
arr[y][x] = '*';
arr[y + 1][x - 1] = '*';
arr[y + 1][x + 1] = '*';
for (int i = x - 2; i <= x + 2; i++)
{
arr[y + 2][i] = '*';
}
}
else
{
draw(x, y, n / 2);
draw(x - n / 2, y + n / 2, n / 2);
draw(x + n / 2, y + n / 2, n / 2);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie();
cout.tie();
cin >> N;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N*2; j++)
{
arr[i][j] = ' ';
}
}
draw(N - 1, 0, N);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N * 2; j++)
{
cout << arr[i][j];
}
cout << '\n';
}
return 0;
}
728x90
반응형
'알고리즘 > solved.ac' 카테고리의 다른 글
[class4] (백준 10830) 행렬 제곱 (0) | 2022.03.08 |
---|---|
[class4] (백준 9935) 문자열 폭발 (0) | 2022.03.07 |
[class4] (백준 2096) 내려가기 (0) | 2022.03.04 |
[class4] (백준 1504) 특정한 최단 경로 (0) | 2022.03.04 |
[class4] (백준 1043) 거짓말 (0) | 2022.03.03 |