Problem L: CreatorX背英语
Time Limit: 1 Sec Memory Limit: 64 MB
Submit: 53 Solved: 36
[Submit][Status][Web Board]
Description
CreatorX最近在忙着背英语,
Hzk is a young and beautiful and cute boy
但是正常背诵的效果他觉得不好,于是他又想了一个点子,倒着背。
Boy cute and beautiful and young a is Hzk
现在给他给你一个只包含大小写字母和逗号(逗号替代空格)的英文句子,单词被一个逗号隔开,让你跟着一起倒着背出来,
Input
第一行一个数字T代表测试的组数。(T<=20)
对于每组测试一行只由大小写字母和逗号组成字符串s(0<|s|<=2000),保证没有连着的逗号,而且句子的第一个字母和最后一个字母不是逗号.
Output
对于每组测试输出一行,把句子以单词为单位倒着输出.
Sample Input
2
Hzk,is,a,young,and,beautiful,and,cute,boy
abc,def
Sample Output
boy,cute,and,beautiful,and,young,a,is,Hzk
def,abc
HINT
还是要的,输入的时候最好不要用getchar(),由于环境问题可能会出奇奇怪怪的错误。
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
t++;
while(t--){
string s,buf;
getline(cin,s);
stack<string>st;
while(!st.empty()){
st.pop();
} //清空!
for(int i=0;i<s.size();i++){
if(s[i]==','){
s[i]=' ';
}
}
int k=0;
stringstream ss(s);
while(ss>>buf){
st.push(buf);
k++;
}
int f=0;
while(!st.empty()){
f++;
if(f==k){
cout<<st.top()<<endl;
}
else{
cout<<st.top()<<",";
}
st.pop();
}
}
}
需要注意stringstream 的用法!