题解 | #字符串排序#

字符串排序

https://www.nowcoder.com/practice/dfeed0e0e4624814b122265e859783b2

//sort函数对输入的字符串长度排序
#include "stdio.h"
#include "string"
#include "algorithm"
using namespace std;
struct charNode{
    int length;
    string str;
};

bool cmp(charNode lhs,charNode rhs){
    if (lhs.length < rhs.length)
        return true;
    return false;
}

int main(){
    int n;char buf[110];charNode array[1000];
    while (scanf("%d\n",&n)!=EOF){
        int i;
        for (i = 0; i < n; ++i) {
            fgets(buf,110,stdin);
            string str = buf;str.pop_back();
            if (str == "stop")
                break;
            array[i].str = str;
            array[i].length = str.size();
        }
        sort(array,array+i, cmp);
        for (int j = 0; j < i; ++j) {
            printf("%s\n",array[j].str.c_str());
        }
    }
}

全部评论

相关推荐

09-23 08:41
已编辑
门头沟学院 Java
牛客吹哨人:可恶!它越来越嚣张了...哨哥晚点统一更新到黑名单:能救一个是一个!26届毁意向毁约裁员黑名单https://www.nowcoder.com/discuss/1525833
点赞 评论 收藏
分享
故事和酒66:央企,事少钱少稳定福利好,离家近可以考虑
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务