字符串排序

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
struct str
{
char a[105];
int len;
};
bool cmp(str x,str y)
{
return x.len <y.len;
}
int main ()
{
int n;
while(scanf("%d\n",&n)!=EOF)
{
int i,j;
str s[10005];
for(i=0;i<n;i++)
{
char c[105];
gets(s[i].a);
if(s[i].a[0] =='s'&&s[i].a[1]=='t'&&s[i].a [2]=='o'&&s[i].a [3]=='p')
break;
s[i].len =strlen(s[i].a );
}
sort(s,s+i,cmp);
for(j=0;j<i;j++)
cout<<s[j].a <<endl;
}
return 0;
用gets()的时候,记得吃掉空格

posted @ 2018-07-28 11:07  换牙  阅读(51)  评论(0编辑  收藏  举报