HDU 1062:字符串反转
【题目来源】
http://acm.hdu.edu.cn/showproblem.php?pid=1062
【题目描述】
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
【输入格式】
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
【输出格式】
For each test case, you should output the text which is processed.
【输入样例】
3
olleh !dlrow
m'I morf .udh
I ekil .mca
【输出样例】
hello world!
I'm from hdu.
I like acm.
【算法代码】
#include <iostream>
#include <stack>
using namespace std;
int main() {
char ch;
int n;
cin>>n;
getchar(); //吸收cin>>n输入之后的换行符
while(n--) {
stack<char> st;
while(true) {
ch=getchar();
if(ch==' ' || ch=='\n') {
while(!st.empty()) {
cout<<st.top();
st.pop();
}
if(ch==' ') cout<<' ';
else break;
} else st.push(ch);
}
cout<<endl;
}
return 0;
}
/*
in:
3
olleh !dlrow
m'I morf .udh
I ekil .mca
out:
hello world!
I'm from hdu.
I like acm.
*/
【参考文献】
https://blog.csdn.net/diaobin0154/article/details/101660651