题解 | 字符串排序

字符串排序

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

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

bool cmp(string& s1, string& s2) {
    return s1.length() <= s2.length();
}

int main() {
    int k;
    while (cin >> k) { // 注意 while 处理多个 case
        cin.ignore();
        string s1;
        string s2;
        string s3 = "stop";
        vector<string>v1;
        for (int i = 0; i < k; i++) {
            getline(cin, s1);
            if (s1 != s3)v1.push_back(s1);
            else break;
        }
        sort(v1.begin(), v1.end(), cmp);
        for (auto it = v1.begin(); it != v1.end(); it++)
            printf("%s\n", (*it).c_str());
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

牛客36400893...:我不是这个专业的,但是简历确实没有吸引我的亮点,而且废话太多没耐心看
0offer是寒冬太冷还...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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