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.
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. 这一题真他妈的恶心,本来是可以用sstream库函数的但是由于输入的字符串可能有多个空格也的输出,却不能用郁闷。。AC代码:#include<iostream> #include<cstring> #include<sstream> using namespace std; int main(){ int t; char a[1005],b[1005]; scanf("%d%*c",&t); while(t--){ gets(a); int i=0; while(a[i]){ int k=0; if(a[i]==' '){ printf(" "); i++; } while(a[i]!=' ' && a[i]) b[k++]=a[i++]; b[k]='\0'; for(int j=strlen(b)-1;j>=0;j--) printf("%c",b[j]); } puts(""); } return 0; }
第一次代码:#include<iostream> #include<set> #include<cmath> #include<string.h> #include<string> #include<algorithm> #include<sstream> using namespace std; int main() { int T; scanf("%d",&T); char ch=getchar(); for(int k=1;k<=T;++k) { string s,str; getline(cin,s); istringstream stream(s); int tot=0; while(stream>>str) { if(tot++) str+=" "; reverse(str.begin(),str.end()); cout<<str; } cout<<endl; }return 0; }