先输入你要输入的字符串的个数。然后换行输入该组字符串。每个字符串以回车结束,每个字符串少于一百个字符。如果在输入过程中输入的一个字符串为“stop”,也结束输入。
然后将这输入的该组字符串按每个字符串的长度,由小到大排序,按排序结果输出字符串。
Input
字符串的个数,以及该组字符串。每个字符串以‘\n’结束。如果输入字符串为“stop”,也结束输入.
Output
将输入的所有字符串按长度由小到大排序输出(如果有“stop”,不输出“stop”)。
Sample Input
5 sky is grey cold very cold stop 3 it is good enough to be proud of good it is quite good
Sample Output
cold very cold sky is grey good it is quite good it is good enough to be proud of
没什么好说的,就是练习c++的多组字符串输入-==--
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
bool cmp(string a,string b){
return a.length()<b.length();
}
int main(){
vector<string> st;
int n;
while(cin>>n&&n){
cin.get();
st.clear();
string s;
while((n-->0)&&getline(cin,s)&&s!="stop")
st.push_back(s);
sort(st.begin(),st.end(),cmp);
for(int i = 0;i < st.size();i++)
cout<<st[i]<<endl;
}
return 0;
}
借鉴用法