거울상 (백준 4583)

코딩생활·2023년 11월 15일
0

백준문제풀이

목록 보기
59/308

안녕하세요. 오늘은 거울로 뒤집을 거예요.

문제

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

아이디어

문제에 나온대로 바꿀 수 있으면 바꿔서 다른 변수에 저장을 해줍시다. 그리고 맨 끝까지 가능하다면 그 변수에 있는 문자열을 뒤집어서 출력해주면 됩니다.

소스코드

#include <string>
#include <algorithm>
using namespace std;

int main(void)
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    string s, ans;

    while (true)
    {
        cin >> s;
        if (s == "#") break;
        ans = "";
        for (char c : s)
        {
            if (c == 'b') ans += 'd';
            else if (c == 'd') ans += 'b';
            else if (c == 'p') ans += 'q';
            else if (c == 'q') ans += 'p';
            else if (c == 'i' || c == 'o' || c == 'v' || c == 'w' || c == 'x') ans += c;
            else
            {
                cout << "INVALID\n";
                break;
            }
        }
        if (s.length() == ans.length())
        {
            reverse(ans.begin(), ans.end());
            cout << ans << "\n";
        }
    }
}


감사합니다~~!

0개의 댓글