文章

28

粉丝

226

获赞

51

访问

131.1k

头像
每个规则制定一种策略
Sacan SVIP
P1255 北京大学机试题
发布于2022年6月5日 23:04
阅读数 5.9k

规则1策略:

在排序比较时,临时将字符统一转成小写在比较,就可以不区分大小写排序。

规则2策略:

stable_sort()

规则3策略:

分开处理。将字母按上述策略排序。对于非字母字符,写一个结构体记录每个字符的值和下标并存起来。将字母单独排序完后,再将每一个非字母字符按照下标插入到排序好的字母序列中。

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

struct teshu{
    int index;
    char value;
};

bool cmp(char x, char y){
    char temp_x;
    char temp_y;
    // 比较时统一临时化成小写
    if('A' <= x && x <= 'Z'){
        temp_x = x + 32;
    }else{
        temp_x = x;
    }
    if('A' <= y && y <= 'Z'){
        temp_y = y + 32;
    }else{
        temp_y = y;
    }

    return int(temp_x) < int(temp_y);
}

int main()
{
    string s;
    vector<char> s1;
    vector<teshu> s2;
    while(getline(cin, s)){
        for(int i = 0;i < s.size();i++){
            if(('a' <= s[i] && s[i] <= 'z') || ('A' <= s[i] && s[i] <= 'Z')){
                s1.emplace_back(s[i]);
   ...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发