취업/Algorithm.
백준 2448. 별 찍기 - 11
Azderica
2020. 3. 3. 18:14
별 찍기 - 11
Problem?
예제를 보고 규칙을 유추한 뒤에 별을 찍어 보세요.
Input
첫째 줄에 N이 주어진다. N은 항상 3×2k 수이다. (3, 6, 12, 24, 48, ...) (k ≤ 10)
Output
첫째 줄부터 N번째 줄까지 별을 출력한다.
Solution
규칙 찾기
규칙을 찾아서 재귀함수를 이용해 풀자.
현재의 높이와, 삼각형의 제일 높은 지점의 위치를 x, y로 지정하고 인자로 넘겨 주었다.
가정 arr : 출력배열 |
Code
#include <iostream>
using namespace std;
char arr[3072][6144];
void star(int height, int x, int y) {
if (height == 3) {
arr[y][x] = '*';
arr[y + 1][x - 1] = '*';
arr[y + 1][x + 1] = '*';
arr[y + 2][x - 2] = '*';
arr[y + 2][x - 1] = '*';
arr[y + 2][x] = '*';
arr[y + 2][x + 1] = '*';
arr[y + 2][x + 2] = '*';
return;
}
star(height / 2, x, y);
star(height / 2, x - (height / 2), y + (height / 2));
star(height / 2, x + (height / 2), y + (height / 2));
}
int main() {
int n, i, j;
cin >> n;
star(n, n - 1, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2 * n; j++) {
if (arr[i][j] == '*')
cout << '*';
else
cout << ' ';
}
cout << "\n";
}
return 0;
}
규칙문제.