0
点赞
收藏
分享

微信扫一扫

字符串排序

杰森wang 2022-04-13 阅读 103
c++

对于给定的 nn 个字符串,进行如下的排序:

  • 首先按字符串长度从小到大排序
  • 当字符串长度相同时,按字典序从小到大排序

现在,你需要编程实现这样的排序功能。

输入格式

输入有 n + 1n+1 行:

  • 第一行为一个整数 nn,0 < n \le 100000<n≤10000;

  • 接下来 nn 行,每行有一个字符串,字符串的长度均不超过 10001000。

输出格式

输出 nn 行,依次为按照题目要求排序后的字符串,每行一个。

格式说明

输出时每行末尾的多余空格,不影响答案正确性

样例输入

5
abcd
abc
go
jisuanke
zero

样例输出

go
abc
abcd
zero
jisuanke
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool cmp(string a,string b){
    if(a.size()!=b.size()){
        return a.size()<b.size();
    }
    return a<b;
}
int main(){
    int n;
    cin>>n;
    string a[10000];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    sort(a,a+n,cmp);
    for(int i=0;i<n;i++){
        cout<<a[i]<<"\n";
    }
    return 0;
}
举报

相关推荐

0 条评论