※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 26547번 문제인 Square이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/26547
26547번: Square
Your mother has asked you to create a template for the squares of a quilt she is making. Her quilt will be based on words! Print out a square of the given word, in the style shown in the example output.
www.acmicpc.net
주어진 문자열을 좌상단 점에서 오른쪽방향과 아래쪽 방향으로, 우하단 점에서 위쪽 방향과 왼쪽 방향으로 작성한 사각형을 출력해 문제를 해결하자.
글쓴이는 첫 행과 마지막 행은 주어진 문자열을 그대로 출력 및 뒤집어서 출력하였고 그 사이 행은 인덱스를 접근해 각각 필요한 문자와 (문자열길이-2)개의 공백을 출력해 문제를 해결했다.
위와 같이 구현할 경우 문자열의 길이가 1인 보더케이스의 예외처리에 유의하자.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int T;
string s;
int slen;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> T;
while (T--) {
cin >> s; slen = s.length();
if (slen == 1) cout << s << '\n';
else {
cout << s << '\n';
for (int i = 1; i + 1 < slen; i++) {
cout << s[i];
for (int j = 2; j < slen; j++) cout << ' ';
cout << s[slen - i - 1] << '\n';
}
reverse(s.begin(), s.end());
cout << s << '\n';
}
}
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 26536 // C++] Cowspeak (0) | 2022.12.21 |
---|---|
[BOJ 26560 // C++] Period (0) | 2022.12.21 |
[BOJ 26510 // C++] V for Vendetta (0) | 2022.12.21 |
[BOJ 22421 // C++] Koto Municipal Subway (0) | 2022.12.21 |
[BOJ 26509 // C++] Triangle (0) | 2022.12.21 |