文章

7

粉丝

0

获赞

14

访问

235

头像
在前人的基础上又优化了一下,应该是最简易懂了
P1255 北京大学机试题
发布于2025年3月12日 20:57
阅读数 15

#include <bits/stdc++.h>
using namespace std;
bool compare(char c, char b) {
    return tolower(c) < tolower(b);  // 忽略大小写排序
}
int main() {
    string s;
    while (getline(cin, s)) {
        string c;
        for (char x : s) {
            if (isalpha(x)) c += x;
        }
        stable_sort(c.begin(), c.end(), compare);
        int j = 0;
        for (char x : s) {
            if (isalpha(x)) cout << c[j++];  // 替换字母
            else cout << x;  // 非字母直接输出
        }
        cout << endl;
    }
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发