문제 링크: https://www.acmicpc.net/problem/2446
이상한 쪽으로 고민의 방향을 틀어버려서 의외로 오래 걸렸던 문제였습니다.
2523번 문제와 비슷하게 풀었지만 이 문제는 공백과 별표를 함께 넣는 문제였기 때문에 for 문을 돌리기 전 공백의 개수와 별표의 개수를 초기화하고 반복문이 한번 실행될 때마다 각 개수를 줄이거나 늘리는 방식으로 짰습니다.
다만 이 방법은 별 개수를 초기화하는 부분이 비효율적인 거 같아 많이 아쉽네요.
#include <iostream>
using namespace std;
int main()
{
int line_length_half;
cin >> line_length_half;
int star_length = 1;
for (int i = 1; i < line_length_half; i++)
{
star_length += 2;
}
int whitespace_length = 0;
int max_length = line_length_half * 2 - 1;
for (int i = 1; i <= max_length; i++)
{
string s = "";
s.insert(0, whitespace_length, ' ');
s.insert(whitespace_length, star_length, '*');
cout << s << endl;
if (i < line_length_half)
{
whitespace_length++;
star_length -= 2;
}
else
{
whitespace_length--;
star_length += 2;
}
}
}