끄적끄적 코딩
article thumbnail

별 찍기 문제입니다.

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

 

#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;
}

'알고리즘' 카테고리의 다른 글

[C++] 백준 13565번 침투  (0) 2019.10.01
[C++] 백준 14954번 Happy Number  (0) 2019.10.01
[C++] 백준 10991번 별 찍기 - 16  (0) 2019.10.01
[C++] 백준 10990번 별 찍기 - 15  (0) 2019.10.01
[C++] 백준 2556번 별 찍기 - 14  (0) 2019.10.01

검색 태그