문제 링크입니다: https://www.acmicpc.net/problem/13015
13015번: 별 찍기 - 23
예제를 보고 규칙을 유추한 뒤에 별을 찍어 보세요.
www.acmicpc.net
세 가지 규칙을 파악해야하는 별 찍기 문제였습니다.
1. 맨위와 맨아래
2. 가운데
3. 그리고 나머지
나머지의 경우 대칭이기 때문에 절댓값인 abs 함수를 사용해야했습니다.
별 찍기의 문제 경우 맨 끝 * 이후 공백이 있으면 WA 받기 때문에 주의해야합니다!
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> | |
#include <algorithm> | |
using namespace std; | |
int main(void) | |
{ | |
int N; | |
cin >> N; | |
for (int i = 0; i < 2 * N - 1; i++) | |
{ | |
// 맨위 맨 아래 | |
if (i == 0 || i == 2 * N - 2) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
cout << "*"; | |
} | |
for (int j = 0; j < 2 * N - 3; j++) | |
{ | |
cout << " "; | |
} | |
for (int j = 0; j < N; j++) | |
{ | |
cout << "*"; | |
} | |
cout << "\n"; | |
continue; | |
} | |
// 가운데 | |
if (i == N - 1) | |
{ | |
for (int j = 0; j < N - 1;j++) | |
{ | |
cout << " "; | |
} | |
cout << "*"; | |
for (int j = 0; j < N - 2; j++) | |
{ | |
cout << " "; | |
} | |
cout << "*"; | |
for (int j = 0; j < N - 2; j++) | |
{ | |
cout << " "; | |
} | |
cout << "*\n"; | |
continue; | |
} | |
// 나머지 | |
for (int j = 0; j < (N - 1) - abs(N - 1 - i); j++) | |
{ | |
cout << " "; | |
} | |
cout << "*"; | |
for (int j = 0; j < N - 2; j++) | |
{ | |
cout << " "; | |
} | |
cout << "*"; | |
for (int j = 0; j < 2 * abs((N - 1) - i) - 1; j++) | |
{ | |
cout << " "; | |
} | |
cout << "*"; | |
for (int j = 0; j < N - 2; j++) | |
{ | |
cout << " "; | |
} | |
cout << "*\n"; | |
} | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 17088번 등차수열 변환 (0) | 2020.04.17 |
---|---|
백준 2522번 별 찍기 - 12, 백준 2523번 별 찍기 - 13 (0) | 2020.04.10 |
백준 10997번 별 찍기 - 22 (0) | 2020.04.09 |
백준 10994번 별 찍기 - 19 (0) | 2020.04.08 |
백준 10993번 별 찍기 - 18 (0) | 2020.04.07 |