游客 Signup | Login
中文 | En

1611 - 字符串排序

先输入你要输入的字符串的个数。然后换行输入该组字符串。每个字符串以回车结束,每个字符串少于一百个字符。

如果在输入过程中输入的一个字符串为“stop”,也结束输入。
然后将这输入的该组字符串按每个字符串的长度,由小到大排序,按排序结果输出字符串。

Input

字符串的个数,以及该组字符串。每个字符串以‘\n’结束。如果输入字符串为“stop”,也结束输入.

Output

可能有多组测试数据,对于每组数据,

将输入的所有字符串按长度由小到大排序输出(如果有“stop”,不输出“stop”)。

Examples

Input

4
faeruhyytrjh tjytj
hsrthts   hjnshtgfhs
stop
3
htrskbns
bsartanjsf tyjndyt
nsr jj jtey

Output

faeruhyytrjh tjytj
hsrthts   hjnshtgfhs
htrskbns
nsr jj jtey
bsartanjsf tyjndyt

Hint

根据输入的字符串个数来动态分配存储空间(采用new()函数)。每个字符串会少于100个字符。

测试数据有多组,注意使用while()循环输入。

Solution C

#include <stdio.h>
#include <string.h>
struct Ch
{
	char str[100];
	int l;
};
int main()
{
	void sort(struct Ch a[],int m);
	struct Ch a[1000];
	int i,n,m;
	char b[5]={"stop"}; 
	while (scanf ("%d",&n)!=EOF)
	{
		m=0;
		getchar();
		for (i=0;i<n;i++)
		{
			gets(a[i].str);
			if (strcmp(b,a[i].str)==0) break;
			m++;
			a[i].l=strlen(a[i].str);
			a[i].str[a[i].l]='\0';
		}
		sort(a,m);
		for (i=0;i<m;i++)
			puts(a[i].str);
	}
	return 0;
}
void sort(struct Ch a[],int m)
{
	int i,j;
	struct Ch t;
	for (i=0;i<m-1;i++)
		for (j=0;j<m-1-i;j++)
			if (a[j].l>a[j+1].l)
			{
				t=a[j]; a[j]=a[j+1]; a[j+1]=t;
			}
}

Solution C++

//我这个是ac了的

#include <iostream>
#include <string>

using namespace std;

typedef struct{
string str;
int len;
}Sqstack; //随便取得名字

int main()
{
        int n;
        Sqstack *a[1000];
        while(cin>>n){
		if(n==0) return 0; 
        cin.ignore();//后面有getline函数一定要ingnore
        string s;
        for(int i=0;i<n;i++)
        {
                getline(cin,s);
                if(s=="stop")
                {
                        n=i;
                        break;

                }

                else{
                a[i]=new Sqstack;
                a[i]->str=s;
                
                a[i]->len=s.length();
                }
        }//(for)

        Sqstack *b;
        for(int i=0;i<n;i++)
                for(int j=0;j<(n-1);j++)
                {
                        if((a[j]->len)>(a[j+1]->len))
                        {
                                b=a[j];
                                a[j]=a[j+1];
                                a[j+1]=b;
                        }
                            
                }
        

for(int i=0;i<n;i++)
        {
                cout<<a[i]->str<<endl;
        }
}//(while)
        return 0;
}

Hint

根据输入的字符串个数来动态分配存储空间(采用new()函数)。每个字符串会少于100个字符。

测试数据有多组,注意使用while()循环输入。

Time Limit 1 second
Memory Limit 32 MB
Discuss Stats
上一题 下一题