7-1 英文单词排序 (25 分)

本题要求编写程序,输入若干英文单词,对这些单词按长度从小到大排序后输出。如果长度相同,按照输入的顺序不变。

输入格式:

输入为若干英文单词,每行一个,以#作为输入结束标志。其中英文单词总数不超过20个,英文单词为长度小于10的仅由小写英文字母组成的字符串。

输出格式:

输出为排序后的结果,每个单词后面都额外输出一个空格。

输入样例:

blue
red
yellow
green
purple
#

输出样例:

red blue green yellow purple 

sort排序不能保证 等长的情况下 按输入的顺序进行输出。 

#include 
#include
#include
using namespace std;
const int maxn=1010;
 
int main(){
 
 	int n=0;
 	string str[maxn],s;
 	
 	while(cin>>s,s!="#"){
 		
 		str[n++]=s;
	 }
	 
	for(int i=1;i<=n-1;i++){
		for(int j=0;j<=n-i-1;j++){
			
			if(str[j].length()>str[j+1].length()){
				
				swap(str[j+1],str[j]);
			}
		 
			
		}
	} 
	
	for(int i=0;i

 

你可能感兴趣的:(天梯)