10994 별 찍기 - 19
2019. 10. 16. 00:35ㆍ알고리즘/백준
재귀 함수로 base 케이스까지 규칙을 찾아서 출력하는 문제. 출력에서 주의할 점은 ' '과 \0은 콘솔에서는 공백처럼 같게 출력되지만 실제 채점 시스템에서는 ' ' 출력을 요구하고 있다
문제: https://www.acmicpc.net/problem/10994
깃허브주소: https://github.com/surinoel/boj/blob/master/10994.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
char mat[500][500]; | |
void print(int n, int x, int y) { | |
if (n == 1) { | |
mat[x][y] = '*'; | |
return; | |
} | |
int size = 4 * (n - 1) + 1; | |
for (int j = y; j < y + size; j++) { | |
mat[x][j] = '*'; | |
mat[x + size - 1][j] = '*'; | |
} | |
for (int i = x; i < x + size; i++) { | |
mat[i][y] = '*'; | |
mat[i][y + size - 1] = '*'; | |
} | |
print(n - 1, x + 2, y + 2); | |
return; | |
} | |
int main(void) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(nullptr); | |
int n; | |
cin >> n; | |
for (int i = 0; i < 4 * (n - 1) + 1; i++) { | |
for (int j = 0; j < 4 * (n - 1) + 1; j++) { | |
mat[i][j] = ' '; | |
} | |
} | |
print(n, 0, 0); | |
for (int i = 0; i < 4 * (n - 1) + 1; i++) { | |
for (int j = 0; j < 4 * (n - 1) + 1; j++) { | |
cout << mat[i][j]; | |
} | |
cout << '\n'; | |
} | |
cout << '\n'; | |
return 0; | |
} |
'알고리즘 > 백준' 카테고리의 다른 글
2010 플러그 (0) | 2019.10.18 |
---|---|
1918 후위 표기식 (0) | 2019.10.17 |
2696 중앙값 구하기 (0) | 2019.10.13 |
1655 가운데를 말해요 (0) | 2019.10.13 |
1715 카드 정렬하기 (0) | 2019.10.13 |