杭电ACM1062(stack)
Problem Description
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.
Input
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.
Output
For each test case, you should output the text which is processed.
Sample Input
3
olleh !dlrow
m’I morf .udh
I ekil .mca
Sample Output
hello world!
I’m from hdu.
I like acm.
题意思路:
题目大致让我们翻转给出的单词注意是一个一个的单词,这里我们用STL里面的stack容器也就是栈(先进后出),每次读入一个单词然后输出;
#include<bits/stdc++.h>
using namespace std;
#include<stack>
int main()
{
char ch;
int n;
scanf("%d",&n);
getchar();
while(n--){
stack<char>s;
while(true){
ch=getchar();
if(ch==' '||ch=='\n'||ch==EOF){
while(!s.empty()){
printf("%c",s.top());
s.pop();
}
if(ch=='\n'||ch==EOF)
break;
printf(" ");
}
else
s.push(ch);
}
printf("\n");
}
return 0;
}
/*
3
olleh !dlrow
m'I morf .udh
I ekil .mca
hello world!
I'm from hdu.
I like acm.
*/;