2133:字符串排序

Description

先输入你要输入的字符串的个数。然后换行输入该组字符串。每个字符串以回车结束,每个字符串少于一百个字符。
如果在输入过程中输入的一个字符串为“stop”,也结束输入。
然后将这输入的该组字符串按每个字符串的长度,由小到大排序,按排序结果输出字符串。

Input

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

Output

可能有多组测试数据,对于每组数据,
将输入的所有字符串按长度由小到大排序输出(如果有“stop”,不输出“stop”)。

Sample Input

4faeruhyytrjh tjytjhsrthts hjnshtgfhsstop3htrskbnsbsartanjsf tyjndytnsr jj jtey

Sample Output

faeruhyytrjh tjytjhsrthts hjnshtgfhshtrskbnsnsr jj jteybsartanjsf tyjndyt

HINT


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

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

Source

#include<bits/stdc++.h>
using namespace std;
bool cmp(string a, string b)
{
    return a.size() < b.size();
}
int main()
{
    
    int count;
    while(cin >> count&&count)
    {
        string *a = new string[count];
        int count1 = 0,c[100],i=0;
        cin.get();
        for(int i=0;i<count;i++)
        {
            getline(cin, a[i]);
            if (a[i] == "stop")
                break;
                count1++;
            }
        sort(a, a + count1, cmp);
        for (int i=0;i<count1;i++)
            cout << a[i] << endl;
        delete[]a;
    }
    system("pause");
}




猜你喜欢

转载自blog.csdn.net/zhangao230/article/details/80209949