티스토리 뷰
문제 링크
https://www.acmicpc.net/problem/10993
10993번: 별 찍기 - 18
예제를 보고 규칙을 유추한 뒤에 별을 찍어 보세요.
www.acmicpc.net
구현 코드
This file contains 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 <cmath> | |
using namespace std; | |
const int MAX = 1024; | |
char Map[MAX][MAX * 2]; | |
void draw_stars(int depth, int x, int y){ | |
if (depth == 1){ | |
Map[y][x] = '*'; | |
return; | |
} | |
int row = pow(2, depth + 1) - 3; | |
int col = pow(2, depth) - 1; | |
//depth가 짝수일 때는 밑을 향한 삼각형을 만든다. | |
if (depth % 2 == 0){ | |
//삼각형의 가장 윗부분을 만든다. | |
for (int i = x; i < row + x; i++){ | |
Map[y][i] = '*'; | |
} | |
//위에서 밑으로 내려가며 만든다. | |
for (int i = 1; i < col; i++){ | |
Map[y + i][x + i] = '*'; | |
Map[y + i][x + row - i - 1] = '*'; | |
} | |
draw_stars(depth - 1, pow(2, depth - 1) + x, y + 1); | |
} | |
//depth가 홀수일 때는 위를 향한 삼각형을 만든다. | |
else{ | |
//삼각형의 가장 밑부분을 만든다. | |
for (int i = x; i < row + x; i++){ | |
Map[y + col - 1][i] = '*'; | |
} | |
//밑에서 위로 올라가며 만든다. | |
for (int i = 0; i < col; i++){ | |
Map[y + i][x + row / 2 - i] = '*'; | |
Map[y + i][x + row / 2 + i] = '*'; | |
} | |
draw_stars(depth - 1, pow(2, depth - 1) + x, col / 2 + y); | |
} | |
return; | |
} | |
int main(){ | |
int n; | |
cin >> n; | |
if (n == 1){ | |
cout << '*' << endl; | |
return 0; | |
} | |
int row = pow(2, n + 1) - 3; | |
int col = pow(2, n) - 1; | |
for (int i = 0; i < col; i++) | |
for (int j = 0; j < row; j++) | |
Map[i][j] = ' '; | |
draw_stars(n, 0, 0); | |
//각 라인마다 마지막 별표가 오는지점까지만 출력해야한다. | |
if (n % 2 == 0){ | |
for (int i = 0; i < col; i++){ | |
for (int j = 0; j < row - i; j++){ | |
cout << Map[i][j]; | |
} | |
cout << endl; | |
} | |
} | |
else{ | |
for (int i = 0; i < col; i++){ | |
for (int j = 0; j < row - col + i + 1; j++){ | |
cout << Map[i][j]; | |
} | |
cout << endl; | |
} | |
} | |
return 0; | |
} |
This file contains 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
def draw_stars(depth, x, y): | |
if depth == 1: | |
Map[y][x] = '*' | |
return | |
r = pow(2, depth+1) - 3 | |
c = pow(2, depth) - 1 | |
if depth % 2 == 0: | |
for i in range(x,r+x): | |
Map[y][i] = '*' | |
for i in range(1,c): | |
Map[y+i][x+i] = '*' | |
Map[y+i][x+r-i-1] = '*' | |
draw_stars(depth-1, pow(2, depth-1) + x, y+1) | |
else: | |
for i in range(x,r+x): | |
Map[y+c-1][i] = '*' | |
for i in range(c): | |
Map[y+i][x+int(r/2)-i] = '*' | |
Map[y+i][x+int(r/2)+i] = '*' | |
draw_stars(depth-1, pow(2, depth-1) + x, int(c/2) + y) | |
n = int(input()) | |
row = pow(2, n+1) - 3 | |
col = pow(2, n) - 1 | |
Map = [[' '] * row for _ in range(col)] | |
draw_stars(n,0,0) | |
if n % 2 == 0: | |
for i in range(col): | |
for j in range(row-i): | |
print(Map[i][j], end="") | |
print() | |
else: | |
for i in range(col): | |
for j in range(row-col+i+1): | |
print(Map[i][j], end="") | |
print() |
'Coding Test > 백준' 카테고리의 다른 글
[C++/Python] 백준 10995 - 별찍기 - 20 (0) | 2020.07.14 |
---|---|
[C++/Python] 백준 10994 - 별찍기 - 19 (0) | 2020.07.13 |
[C++/Python] 백준 10992 - 별찍기 - 17 (0) | 2020.07.11 |
[C++/Python] 백준 10991 - 별찍기 - 16 (0) | 2020.07.10 |
[C++/Python] 백준 10990 - 별찍기 - 15 (0) | 2020.07.09 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 합친 lis
- import
- 알고스팟
- 2225
- Django
- 하둡
- python
- 하이브
- 출전 순서 정하기
- Sqoop
- 완전탐색
- 종만북
- Jaeha's Safe
- pyspark
- 코딩인터뷰 완전분석
- 두니발 박사의 탈옥
- Hadoop
- 배열과 문자열
- HiveQL
- 백준
- 외발 뛰기
- C++
- microwaving lunch boxes
- HDFS
- 삼각형 위의 최대 경로
- hive
- 삼각형 위의 최대 경로 수 세기
- 분할정복
- 스파크
- 팰린드롬 구하기
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함