题解 | #字符串排序#

字符串排序

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

#include <iostream>
#include <algorithm>
using namespace std;
bool compare(char a, char b) {
    if (a >= 'a' && a <= 'z') a = 'A' + a - 'a';
    if (b >= 'a' && b <= 'z') b = 'A' + b - 'a';
    return a < b;
}
bool isLetter(char c) {
    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
        return true;
    else return false;
}
int main() {
    string a;
    while (getline(cin,a) ) { // 注意 while 处理多个 case
        // cout << a + b << endl;
        string b;
        for (int i = 0; i < a.size(); i++) {
            if (isLetter(a[i])) b += a[i];
        }
        stable_sort(b.begin(), b.end(), compare);
        int j = 0;
        for (int i = 0; i < a.size(); i++) {
            if (isLetter(a[i])) {
                a[i] = b[j];
                j++;
            }
        }
        cout<<a<<endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 11:30
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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