input:
3
olleh !dlrow
m'I morf .udh
I ekil .mca
output:
hello world!
I'm from hdu.
I like acm.
错误代码:
#include<bits/stdc++.h>
using namespace std;
stack<char>s;
int main(){
ios::sync_with_stdio(0);
int n;
cin>>n;
char ch;
while(n--){
while(1){
ch=getchar();
if(ch==' '||ch=='\n'||ch==EOF){
while(!s.empty()){
cout<<s.top();
s.pop();
}
if(ch=='\n'||ch==EOF) break;
cout<<" ";
}
else s.push(ch);
}
cout<<endl;
}
return 0;
}
正确代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; scanf("%d",&n); getchar();
while(n--){
stack<char> s;
while(true){
char 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;
}
求求大佬解答一下输入哪里到底有什么问题啊啊啊