新人对输入问题有点懵。。。
  • 板块学术版
  • 楼主Fionn_Lee
  • 当前回复8
  • 已保存回复8
  • 发布时间2023/2/19 23:29
  • 上次更新2023/10/24 00:16:45
查看原帖
新人对输入问题有点懵。。。
834727
Fionn_Lee楼主2023/2/19 23:29

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;
}

求求大佬解答一下输入哪里到底有什么问题啊啊啊

2023/2/19 23:29
加载中...