https://www.acmicpc.net/problem/12605
아이디어:
1. 스택에 문자열을 잘라 넣고 마지막에 그대로 꺼내서 출력
2. 공백 포함 한줄 문자열 입력 (ex. "this is a test")
string tmp;
// cin.ignore(); // if you already use cin
getline(cin, tmp);
3. 한줄 문자열의 공백으로 잘라서 스택에 입력
string t;
stack<string> str;
stringstream ss(tmp);
while(ss >> t)
str.push(t);
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N;
string tmp, t;
stack<string> str;
cin >> N;
cin.ignore();
for (int i = 0; i < N; i++) {
getline(cin, tmp);
stringstream ss(tmp);
while (ss >> t)
str.push(t);
cout << "Case #" << i + 1 << ": ";
while (!str.empty()) {
cout << str.top() << " ";
str.pop();
}
cout << "\n";
}
return 0;
}
'algorithm' 카테고리의 다른 글
백준 4949번 균형잡힌 세상 (0) | 2025.02.21 |
---|---|
백준 17608번 막대기 (0) | 2025.02.17 |
백준 1874번 스택 수열 (0) | 2025.02.16 |
백준 11003번 최솟값 찾기 - 덱 정렬 (0) | 2024.12.15 |
백준 12891번 DNA 비밀번호 (0) | 2024.12.15 |