Super Kawaii Cute Cat Kaoani [BOJ/백준/C++] 2447번 별 찍기 - 10

[BOJ/백준/C++] 2447번 별 찍기 - 10

2024. 1. 29. 12:18
728x90
SMALL
 

2447번: 별 찍기 - 10

재귀적인 패턴으로 별을 찍어 보자. N이 3의 거듭제곱(3, 9, 27, ...)이라고 할 때, 크기 N의 패턴은 N×N 정사각형 모양이다. 크기 3의 패턴은 가운데에 공백이 있고, 가운데를 제외한 모든 칸에 별이

www.acmicpc.net


 

반응형

📌 접근 방법

😱 쉬워보였지만 매우매우 어려웠다 😅

  • N = 3일 경우 공백 
    (1, 1)
  • N=9일 경우 공백
    (1,1), (1,4), (1,7)
    (3,3), (3,4), (3,5)
    (4,1), (4,3), (4,4), (4,5), (4,7)
    (5,3), (5,4), (5,5)

➡️ 규칙

  • 작은 공백(x%3==1)&&(y%3==1)의 조건을 만족할 때 공백이 생긴다는 것을 알 수 있었다.
  • 큰 공백((x/N)%3==1)&&((y/N)%3==1)이 만족되면 공백생긴다는 것을 알 수 있었다.

재귀적 반복을 이용하면 구할 수 있었다.

 

✅  Pass Code

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<iterator>
#include<map>
#include<set>
#include<unordered_set>
#include <stack>
#include<queue>
#include<deque>

using namespace std;
void star(int n, int x, int y){
    if((x/n)%3==1&&(y/n)%3==1){
        cout<<" ";
    }
    else{
        if(n/3==0){
            cout<<"*";
        }
        else{
            star(n/3, x, y);
        }
    }
}
int main() {

    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int N;
    cin>>N;
    for(int i=0; i<N; i++){
        for(int j=0; j<N; j++){
            star(N,i,j);
        }
        cout<<'\n';

    }


    return 0;
}

 

 

728x90
LIST

BELATED ARTICLES

more