알고리즘 연습

C++ 2442 별찍기 5

728x90

#include <iostream>
using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (i + j < n + 1)
				cout << ' ';
			else if (i + j >= n + 1)
				cout << '*';
		}
		for (int j = 2 * n; j > n; j--) {
			if (i + j > 2 * n + 1)
				cout << '*';
		}
		cout << '\n';
	}
	
}

for문을 사용할 때 같은 변수 j를 두번 사용한다.

삼각형의 모양으로 아래처럼 별을 먼저 고려하고 추가적으로 오른쪽의 삼각형을 고려하여 출력했다.

728x90