<알고리즘 문제풀이&연습>/[C++] 백준, 프로그래머스 등등

[백준 2445] 별찍기8

BlockDMask 2017. 11. 20. 23:30
반응형
  • 안녕하세요. BlockDMask 입니다.

  • 별찍기 시리즈 중 여덟번째 입니다.

  • 시작하겠습니다.
    171026 문제 빼먹음 -> 171120 완료

0. 제목

  • 백준 2445 별찍기8

  • BOJ 2445 별찍기 8

  • C++ 별찍기

1. 문제 설명

  • 모래시계 형태로 별을 찍으면 됩니다.

  • 입력 : N (1<=N<=100)

  • 출력 : 첫째 줄 부터 2*N-1 줄 까지 차례대로 별을 출력하면 됩니다.

  • 입력이 7이 들어오면


2. 풀이 과정

  • 윗부분, 가운데 부분 두단계로 나누었습니다.

  • 아랫부분은 윗부분을 다시 역으로 출력하는 방식을 취했습니다.

  • 처음에 네 부분으로 나누어서, 
    왼쪽위의 부분을 세로로 대칭 가로로 대칭을 해서 4개의 부분으로 쪼갰는데;
    코드의 가독성이 떨어진다고 판단해서
    아래와 같이 작성했습니다.


3. 소스 코드


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//https://www.acmicpc.net/problem/2445
//BOJ_2445_star8
 
#include<iostream>
#include<cstdio>
using namespace std;
 
class star{
private:
    int n;
    void draw(int key, bool isStar) const;
    void drawMid() const;
    void printStar(bool a) const;
public:
    star(int n):n(n){}
 
    void callDraw(bool diamond) const{
        for(int i=0; i<n-1; i++){
            this->draw(i+1!diamond);    //위
        }
        drawMid();
        for(int i=0; i<n-1; i++){
            this->draw(n-i-1!diamond);  //아래
        }
    }
 
};
void star::draw(int key, bool isStar) const{
    for(int i=0; i<key; i++) printStar(isStar);
    for(int j=0; j<(2*n)-(2*key); j++) printStar(!isStar);
    for(int i=0; i<key; i++) printStar(isStar);
    printf("\n");
 
}
void star::drawMid() const{
    for(int i=0; i< 2*n ; i++){
        printf("*");
    }
    printf("\n");
}
 
void star::printStar(bool a) const{
    if(a) printf(" ");
    else printf("*");
}
 
int main(void){
    int n;
    cin >> n;
    star *= new star(n);
    s->callDraw(true);
    delete s;
    return 0;
}
cs


4. 인증


문제 출처 - https://www.acmicpc.net/problem/2445

감사합니다.

반응형