BOJ | 2439번

송치헌·2021년 6월 12일
0
post-thumbnail

Python 풀이

N = int(input())

for i in range(1, N+1):
    a = '*'*i
    print(a.rjust(N)) # rjust(N) : N칸을 확보하고 오른쪽 정렬

a.rjust(N)은 N칸을 확보하고 오른쪽 정렬을 시켜주는 함수이다. 예를 들어

>>> a = 'hello'
>>> print(a.rjust(10))
     hello

이 코드는 전체 10칸을 확보하고 오른쪽 정렬로 hello를 출력하라는 의미이다.
즉, hello는 5자이고, 공백이 5칸이 생기게 된다.

>>> a = 'hello'
>>> print(a.rjust(10,'.'))
.....hello

rjust(10,'.')라고 하게 되면 위에 코드와 똑같은 의미이지만 공백대신 .으로 채운다. default값은 ' '인 것 같다.

C++ 풀이

#include <iostream>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
    	for (int j = 1; j <= n-i; j++) {
    		cout << " ";
    	}
    	for (int k = n-i+1; k <= n; k++) cout << "*";
    	cout << '\n';
    }
}
profile
https://oraange.tistory.com/ 여기에도 많이 놀러와 주세요

0개의 댓글