알고리즘

[C++] 백준 10992번 별 찍기 - 17

J3SUNG 2019. 10. 1. 11:37
728x90

별 찍기 문제입니다.

첫번째 줄과 마지막 줄은 원래대로 출력하고
가운데 줄들은 중간에 공백으로 처리해서 출력해주었습니다.

 

#include <iostream>
#include <algorithm>
using namespace std;

int main(int argc, char* argv[])
{
	int n;

	cin >> n;

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n - i; ++j) {
			cout << " ";
		}
		
		if (i == 1 || i == n) {
			for (int j = 1; j <= (i - 1) * 2 + 1; ++j) {
				cout << "*";
			}
		}
		else {
			cout << "*";
			for (int j = 1; j <= (i - 1) * 2 - 1; ++j) {
				cout << " ";
			}
			cout << "*";
		}
		cout << "\n";
	}

	return 0;
}