티스토리 뷰

문제 링크

https://www.acmicpc.net/problem/2448

 

2448번: 별 찍기 - 11

첫째 줄에 N이 주어진다. N은 항상 3×2k 수이다. (3, 6, 12, 24, 48, ...) (k ≤ 10)

www.acmicpc.net

 

구현 코드

 

import math
s = [' * ', ' * * ', '***** ']
def print_star(shift):
c = len(s)
for i in range(c):
s.append(s[i] + s[i])
s[i] = (" " * shift + s[i] + " " * shift)
n = int(input())
k = int(math.log(int(n / 3), 2))
for i in range(k):
print_star(int(pow(2, i)))
for i in range(n):
print(s[i])
view raw 2448.py hosted with ❤ by GitHub
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
void print_star(int shift, vector<string>& s){
int c = s.size();
for(int i=0;i<c;i++){
s.push_back(s[i]+s[i]);
string tmp;
for(int j=0;j<shift;j++)
tmp += " ";
tmp += s[i];
for(int j=0;j<shift;j++)
tmp += " ";
s[i] = tmp;
}
}
int main(){
vector<string> s;
s.push_back(" * ");
s.push_back(" * * ");
s.push_back("***** ");
int n;
cin >> n;
int k = log(n/3) / log(2);
for(int i=0;i<k;i++){
print_star(int(pow(2,i)), s);
}
for(int i=0;i<n;i++){
cout << s[i] << endl;
}
return 0;
}
view raw 2448_1.cpp hosted with ❤ by GitHub
#include <iostream>
using namespace std;
char arr[3072][3072*2];
void draw(int x, int y, int num){
if(num == 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] = '*';
}
else{
draw(x,y,num/2);
draw(x-num/2,y+num/2,num/2);
draw(x-num/2+num,y+num/2,num/2);
}
}
int main(){
int n;
cin >> n;
for(int i=0;i<n;i++){
for(int j=0;j<n*2-1;j++){
arr[i][j] = ' ';
}
}
draw(n-1,0,n);
for(int i=0;i<n;i++){
for(int j=0;j<n*2-1;j++){
cout << arr[i][j];
}
cout << "\n";
}
}
view raw 2448_2.cpp hosted with ❤ by GitHub