Algorithm 💫/Problem Solving
[백준 2445번 별찍기 - 8/ C++]
돼지고기맛있다
2021. 1. 4. 16:32
반응형
2445번: 별 찍기 - 8
첫째 줄부터 2×N-1번째 줄까지 차례대로 별을 출력한다.
www.acmicpc.net
별찍기 정말 재미있다..ㅎㅅㅎ 예전엔 재미 없었는데 지금 보니까 존잼인 문제였다.
별찍기 - 8 은 그냥 나비넥타이? 같은 모양을 출력해주어야 한다.
단순히 생각해서 양옆을 left right으로 두고 각 변수가 전진하는 칸이 "*"이냐 " "(공백)이냐에 따라서 swaping 해주는 과정을 거쳤다.
그렇게 되면 다음과 같이 출력하게 된다. ㅎㅅㅎ
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> stars(200, " ");
int main()
{
int N;
cin >> N;
int mid = (2 * N - 1) / 2;
int left = 0, right = (2 * N - 1);
stars[left] = "*";
stars[right] = "*";
for (int i = 0; i < (2 * N - 1); i++) {
for (int j = 0; j < 2 * N; j++) {
cout << stars[j];
}
cout << "\n";
left++;
right--;
if (stars[left] == " ")
stars[left] = "*";
else
stars[left] = " ";
if (stars[right] == " ")
stars[right] = "*";
else
stars[right] = " ";
}
return 0;
}
반응형