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


[알고리즘풀이]

위쪽 삼각형과 아래쪽 삼각형을 나눠서 구현한다.

#include<iostream>

using namespace std;

int main() { 
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;

	for (int i = 1; i <= 2 * n - 1; i++) { 
		if (i < n) {
			for (int j = 0; j < n - i; j++)
				cout << ' ';
			for (int j = 0; j < 2 * i - 1; j++)
				cout << '*';
		}
		else {
			for (int j = 0; j < i - n; j++)
				cout << ' ';
			for (int j = 0; j < 4 * n - 2 * i - 1; j++)
				cout << '*';
		}
		cout << '\n';
	}
	return 0; 
}

 

'Problem Solving > BOJ' 카테고리의 다른 글

[BOJ] 2446 - '별 찍기 - 9'  (0) 2019.10.26
[BOJ] 2445 - '별 찍기 - 8'  (0) 2019.10.26
[BOJ] 2443 - '별 찍기 - 6'  (0) 2019.10.26
[BOJ] 2442 - '별 찍기 -5'  (0) 2019.10.26
[BOJ] 2441 - '별 찍기 - 4'  (0) 2019.10.26

+ Recent posts