• 华为OD机考:0023-磁盘容量排序


    0023-磁盘容量从小到大排序

    使用到知识点

    unorder_map

    sort-cmp

    字符串转化

    题目描述

    磁盘的容量单位常用的有
    M G T
    他们之间的换算关系为 1T =1024G 1G=1024M
    现在给定n块磁盘的容量,请对他们按从小到大的顺序进行稳定排序
    例如给定5块盘的容量
    5
    1T
    20M
    3G
    10G6T
    3M12G9M
    排序后的结果为
    20M
    3G
    3M 12G 9M
    1T,10G 6T
    注意单位可以重复出现
    上述3M 12G 9M表示的容量即为 3M 12G 9M 和12M 12G相等

    输入描述

    输入第一行包含一个整数n
    2<=n<=100 表示磁盘的个数
    接下来的n行
    每行一个字符串
    2<长度<30
    表示磁盘的容量
    由一个或多个格式为MV的子串组成
    其中m表示容量大小
    v表示容量单位
    例如20M 1T
    磁盘容量的范围1~1024的正整数
    单位 M G T
    输出n行
    表示n块磁盘容量排序后的结果

    样例

    实例
        输入
        3
        1G
        2G
        1024M
        输出
        1G
        1024M
        2G
        说明:稳定排序要求相等值保留原来位置
    
        示例2
        3
        2G4m
        3M2G
        1T
        输出
        3M2G
        2G4M
        1T
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    C++ 代码

    //
    // Created by HANWENKE on 2022/9/4.
    //
    #include 
    #include 
    #include 
    #include 
    #include 
    using  namespace std;
    unordered_map<int,string>ismap;
    static bool cmp(pair<int,int>&a,pair<int,int>&b){
        //如果容量小的时候-那么就返回真
        if(a.second<b.second){
            return true;
        }else if(a.second==b.second){
            //如果容量相等的时候--比较位置大小-返回真假
            return a.first<b.first;
        }
        //如果a.seconde>b.seconde的时候就返回false
        return false;
    }
    //将容量统一转化为M--进行大小比较--记录一下位置信息
    static int calc(string &str){
        int size=0;
        int index=-1;
        for(int i=0;i<str.length();i++){
            if(str[i]=='M'||str[i]=='m'){
                string ss=str.substr(index+1,i-index-1);
                //atoi将字符串转化为整数--要调用.c_str转化为 const char *
                //size+= atoi(ss.c_str());
                size+= stoi(ss);
                index=i;
            }else if(str[i]=='G'||str[i]=='g'){
                string ss=str.substr(index+1,i-index-1);
                size+= stoi(ss)*1024;
                index=i;
            }else if(str[i]=='T'||str[i]=='t'){
                string ss=str.substr(index+1,i-index-1);
                size+= stoi(ss)*1024*1024;
                index=i;
            }
        }
        return size;
    }
    int main(){
        int n;
        cin>>n;
        string ss;
    
        vector<string>arr;
        arr.reserve(n);
        //为什么要加这一行,因为输入整数之后,整数从缓冲区流到了int型变量n中,此时缓冲区还剩下一个换行字符
        char t=cin.get();
        //getline(cin,ss);
        for(int i=0;i<n;i++){
            getline(cin,ss);
            arr.push_back(ss);
            ss.clear();
        }
        vector<pair<int,int>>arr1;
        for(int i=0;i<arr.size();i++){
            //将字符串转化为整数-并且记录其位置信息放入到二维数组中--进行排序
            int temp=calc(arr[i]);
            //同时记录其位置信息-方便后期进行直接输出-而不用进行转化
            ismap[i+1]=arr[i];
            arr1.push_back(pair<int,int>(i+1,temp));
        }
        sort(arr1.begin(),arr1.end(),cmp);
        for(auto &x:arr1){
            cout<<ismap[x.first]<<endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
  • 相关阅读:
    读《MySQL是怎样运行的》笔记记录
    Mybatis开启日志
    业务调整,业绩短期承压,市场热潮退去后的乐舱物流将驶向何方?
    大数据毕业设计可视化大屏前后端项目分享
    ZooKeeper-实战
    【连网】Win10总是自动断网,检测默认网关不可用
    【无标题】
    JavaScript 变量 及 案例练习
    Java中synchronized:特性、使用、锁机制与策略简析
    vue--push,pop,unshift,shift,reverse,splice,sort数组修改产生视图更新
  • 原文地址:https://blog.csdn.net/weixin_41694574/article/details/126685320