본문 바로가기

백준

9093 단어 뒤집기

자꾸 한 줄을 빼먹길래 찾아보니깐 버퍼 문제였다. 

cin.ignore();

이거 쓰면 버퍼 비울 수 있다길래 썼는데 

 

아래처럼 첫 글자를 지운다???

원인은 나중에 알아봐야겠음

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <bitset>

#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()

using namespace std;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;
using iii = tuple<int, int, int>;

using namespace std;

int n;
string s, backup;
string orignal;

int     main()
{
    int t;
    cin >> t;
    cin.ignore();

    for (int i = 0; i < t; i++)
    {
        string s;
        getline(cin, s);
        cin.ignore();

        string tmp = "";
        for (int j = 0; j < s.size(); j++)
        {
            if (s[j] != ' ')
            {
                tmp += s[j];
                continue;
            }

            reverse(all(tmp));
            cout << tmp << " ";
            tmp = "";
        }

        reverse(all(tmp));
        cout << tmp << " ";
        cout << endl;
    }

    return 0;
}

 

에엥... cin 하고 나서 버퍼만 지우면 됐다.. 

 

알고보니 cin >> t는 개행 전까지 읽는 거고 getline은 개행까지 읽는다

 

그래서 cin >> t 다음 cin.ignore()로 개행 없애준 다음 getline을 하면 정상적으로 돌아감

 

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <bitset>

#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()

using namespace std;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;
using iii = tuple<int, int, int>;

using namespace std;

int n;
string s, backup;
string orignal;

int     main()
{
    int t;
    cin >> t;
    cin.ignore();

    for (int i = 0; i < t; i++)
    {
        string s;
        getline(cin, s);

        string tmp = "";
        for (int j = 0; j < s.size(); j++)
        {
            if (s[j] != ' ')
            {
                tmp += s[j];
                continue;
            }

            reverse(all(tmp));
            cout << tmp << " ";
            tmp = "";
        }

        reverse(all(tmp));
        cout << tmp << " ";
        cout << endl;
    }

    return 0;
}

'백준' 카테고리의 다른 글

2847 게임을 만든 동준이 [미완]  (0) 2020.10.07
16471 작은 수 내기  (0) 2020.10.07
18406 럭키 스트레이트  (0) 2020.10.06
2138 전구와 스위치  (0) 2020.10.06
2877 4와 7  (0) 2020.10.06