文章

16

粉丝

68

获赞

3

访问

7.5k

头像
字符串排序2 题解:c++借鉴某位题解,并加入了自己理解注释
P1255 北京大学机试题
发布于2024年3月10日 22:03
阅读数 1.0k

#include <bits/stdc++.h>
using namespace std;
bool cmp(char a,char b){
    if(a>='a'&&a<='z'&&b>='a'&&b<='z')
        return a<b;
    else if(a>='A'&&a<='Z'&&b>='A'&&b<='Z')
        return a<b;
    else if(a>='a'&&a<='z'&&b>='A'&&b<='Z')
        return a-32<b;
    else if(a>='A'&&a<='Z'&&b>='a'&&b<='z')
        return a<b-32;
}
char c[1000];//用于排序后的新字符串
int f[1000]={0};//用于标记原字符串中i位置是否是符号,0代表为字母,1代表为符号
int main(){
    string s;
    while(getline(cin,s)){
        int len=s.length();
        int cnt=0;//记录新新字符串的长度
        for (int i = 0; i < len; ++i){
            if(s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z')
                c[cnt++]=s[i];
            else
                f[i]=1;//代表s[i]是字符
        }
        stable_sort(c,c+cnt,cmp);
        int num=0;
        for (...
登录查看完整内容


登录后发布评论

1 条评论
机试过过过· VIP
2024年7月26日 15:57

原字符串也不一定小于等于1000,新字符串c不一定长度小于等于1000吧?

赞(0)