[백준] 별찍기 -5 2442

Soohyeon B·2022년 10월 17일
0

알고리즘 문제 풀이

목록 보기
15/70

풀이1

#include <bits/stdc++.h>
using namespace std;


int main (void){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    int n;
    cin >> n;
    int blank = (2*n-1)/2;
    
    //n줄 2n-1행 , 5줄, 9행 9/2 =4
    for(int i=1; i<=n; i++, blank--){ //1 3 5 9 2n-1
        for(int j=0; j<blank; j++) cout << "-";
        for(int j=0; j<2*i-1; j++) cout << "*";
        for(int j=0; j<blank; j++) cout << "-";
        cout << "\n";
    }
    
}

출력 형식이 틀렸다고 한다.
오른쪽 공백은 출력하지 않아야 한다고 함.

제출 2

#include <bits/stdc++.h>
using namespace std;


int main (void){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    int n;
    cin >> n;
    int blank = (2*n-1)/2;
    
    //n줄 2n-1행 , 5줄, 9행 9/2 =4
    for(int i=1; i<=n; i++, blank--){ //1 3 5 9 2n-1
        for(int j=0; j<blank; j++) cout << " ";
        for(int j=0; j<2*i-1; j++) cout << "*";
        cout << "\n";
    }
    
}
profile
하루하루 성장하는 BE 개발자

0개의 댓글