[C++][백준 9093] 단어 뒤집기

PublicMinsu·2025년 8월 18일

문제

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

접근 방법

한 문장을 기준으로 단어를 뒤집어야 하기에 한 문장씩 입력받을 수 있어야 합니다.

코드

#include <iostream>
#include <stack>
using namespace std;

int N;
stack<char> st;

int main()
{
    ios::sync_with_stdio(0), cin.tie(0);

    cin >> N;
    cin.ignore();

    while (N--)
    {
        string str;
        getline(cin, str);

        for (char c : str)
        {
            if (c == ' ')
            {
                while (!st.empty())
                {
                    cout << st.top();
                    st.pop();
                }

                cout << c;
            }
            else
            {
                st.push(c);
            }
        }

        while (!st.empty())
        {
            cout << st.top();
            st.pop();
        }

        cout << "\n";
    }
    return 0;
}

풀이

getline으로 한 문장을 입력받고 단어를 구분하여 뒤집어 주면 됩니다.

profile
연락 : publicminsu@naver.com

0개의 댓글