백준 2442번 (별 찍기 - 5, C++) [BAEKJOON]

문제

https://www.acmicpc.net/problem/2442

시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율
1 초 128 MB 57288 31626 27981 55.406%

첫째 줄에는 별 1개, 둘째 줄에는 별 3개, ..., N번째 줄에는 별 2×N-1개를 찍는 문제

별은 가운데를 기준으로 대칭이어야 한다.

입력

첫째 줄에 N(1 ≤ N ≤ 100)이 주어진다.

출력

첫째 줄부터 N번째 줄까지 차례대로 별을 출력한다.

예제 입력 1

plaintext
5

예제 출력 1

plaintext
    *
   ***
  *****
 *******
*********

알고리즘 분류

출처


통과된 코드

cpp
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int N;
	string str;
	cin >> N;
	for (int i = 0; i < (2 * N) - 1; i++)
		str.push_back(' ');
	for (int i = 0; i < N; i++) {
		str[(str.length() / 2) + i] = '*';
		str[(str.length() / 2) - i] = '*';
		string temp = str.substr(0, (str.length() / 2) + i + 1);
		cout << temp << "\n";
	}

	return 0;
}