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.
输入
输入包含几个测试用例。输入的第一行是一个整数 T,它是测试用例的数量。T测试用例如下。
每个测试用例包含一行,其中包含多个单词。一行最多有 1000 个字符。
输出
对于每个测试用例,您应该输出处理的文本。
Sample Input
3 olleh !dlrow m'I morf .udh I ekil .mca
Sample Output
hello world! I'm from hdu. I like acm.
暗示
记得使用getchar() 读取整数T 后的'\n',然后您可以使用gets() 读取一行并处理它。
题意描述:
对于输入的测试案例,将里面所以单词倒转然后输出
解题思路:
利用栈后进先出,将开始的单词存入栈中,直到遇到第一个空格,将前面的单词输出,对于最后一个单词,最后一个字母后面是换行 所以要单独处理最后一个字母
注意: 注意最后一个单词的处理
AC
#include<stdio.h> #include<string.h> #include<stack> #include<iostream> using namespace std; int main() { int n; cin>>n; getchar();//获取\n while(n--) { char a[1010]; gets(a); stack<char>s; for(int i=0;i<strlen(a);i++) { if(a[i]==' ')//遇到空格,将栈内的单词放出 { while(!s.empty()) { cout<<s.top(); s.pop(); } cout<<" "; } else if(a[i+1]=='\0') //检测最后一个单词 以及最后一个字母 { s.push(a[i]); while(!s.empty()) { cout<<s.top(); s.pop(); } } else { s.push(a[i]);//将原单词一个一个进栈 } } printf("\n"); } return 0; }