带空格的字符串的问题
  • 板块题目总版
  • 楼主sephiloss
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/1/18 14:35
  • 上次更新2023/11/5 04:42:20
查看原帖
带空格的字符串的问题
100579
sephiloss楼主2021/1/18 14:35

这个题
用这个代码就能过

#include<bits/stdc++.h>
using namespace std;
int main(){
	string s,t="";
	queue<string>q;
	while(cin>>s)q.push(s);
	while(q.size()){
		t=q.front();
		q.pop();
		reverse(t.begin(),t.end());
		cout<<t<<endl;
	}
}

但是用下面这个就wa

#include<bits/stdc++.h>
using namespace std;
int main(){
	string s,t="";
	getline(cin,s);
	queue<string>q;
	for(int i=0;i<s.size();i++){
		if(s[i]==' '){
			q.push(t);
			t="";
		}else if(i+1==s.size()){
			t=t+s[i];
			q.push(t);
		}else t=t+s[i];
	}
	while(q.size()){
		t=q.front();
		q.pop();
		reverse(t.begin(),t.end());
		cout<<t<<endl;
	}
}

why?getline有什么坑?

2021/1/18 14:35
加载中...