文章

52

粉丝

68

获赞

22

访问

11.6k

头像
字符串排序2 题解:stablesort,大小写同时排序
P1255 北京大学机试题
发布于2025年1月27日 15:45
阅读数 96

#include <bits/stdc++.h>
using namespace std;

bool compare(char c,char b){
    if(c>='A'&&c<='Z')c=c-'A'+'a';
    if(b>='A'&&b<='Z')b=b-'A'+'a';
    return c<b;
}

int main(){
    string s;
    while(getline(cin,s)){
        string c;
        for(auto x:s){
            if((x>='A'&&x<='Z')||(x>='a'&&x<='z'))c+=x;
        }
        stable_sort(c.begin(),c.end(),compare);
        int j=0;
        for(int i=0;i<s.size();i++){
            char x=s[i];
            if((x>='A'&&x<='Z')||(x>='a'&&x<='z'))cout<<c[j++];
            else cout<<s[i];
        }cout<<endl;
    }
}

我们先把需要排序的提取出来,然后按照原来的s的位置模版进行输出即可

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发