문제 : https://www.acmicpc.net/problem/2446


[알고리즘풀이]

역삼각형 부분과 나머지 부분으로 나누어서 구현하면 됩니다.

역삼각형 부분은 총 n 줄이고 ' ' 는 늘어나고,  '*' 는 줄어드고

아래 부분은 총 n - 1 줄이고 ' ' 는 줄어드고, '*' 는 늘어납니다.

#include<iostream>

using namespace std;


int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	int n;
	cin >> n;
	// 역삼각형
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < i; j++)
			cout << ' ';
		for (int j = 0; j < (2 * n - 1) - 2 * i; j++)
			cout << '*';
		cout << '\n';
	}
	// 나머지부분
	for (int i = 1; i < n; i++) {
		for (int j = 0; j < (n - 1) - i; j++)
			cout << ' ';
		for (int j = 0; j < 2 * i + 1; j++)
			cout << '*';
		cout << '\n';
	}
}

 

+ Recent posts