• 华为补招笔试题20171130


    注:实现时无需考虑不合法的情况。

    解答过程:感觉没有问题,可后来通过率才37.5%,puzzle。

    #include <iostream>
    using namespace std;
    #include <string>
    #include <vector>
    #include <algorithm>
    
    //转化为大写
    string strToUpper(string &str)
    {
        for (int i = 0; i < str.size(); i++)
        {
            if (str[i] >= 97)
            {
                str[i] -= 32;
            }
        }
        return str;
    }
    
    
    int main()
    {
        string strOrder, strNum;
        cin >> strOrder >> strNum;
        int i = 0;
        vector<string> vecstrNum;//用于存放逗号之间的整数字符串
        string strTemNum;
    
        //把逗号之间的整数字符串选出来,并放到vecstrNum中
        while (i < strNum.size())
        {
            if (strNum[i] != ',')
            {
                strTemNum.push_back(strNum[i]);
            }
            else
            {
                vecstrNum.push_back(strTemNum);
                strTemNum.clear();
            }
            i++;
            if (i == strNum.size())
            {
                vecstrNum.push_back(strTemNum);
            }
        }
    
    
        for (int j = 0; j < vecstrNum.size(); j++)
        {
            //对每个整数字符串内部进行升序排序
            sort(vecstrNum[j].begin(), vecstrNum[j].end());
    
            //把第一位是0的进行处理,和后面首个不为0的数字进行交换
            if (vecstrNum[j][0] == '0')
            {
                for (int i = 1; i < vecstrNum[j].size(); i++)
                {
                    if (vecstrNum[j][i] != '0')
                    {
                        swap(vecstrNum[j][0], vecstrNum[j][i]);
                        break;
                    }
                }
            }
        }
    
        //需要把整数字符串转化为long类型或者int类型,因为string比较大小和数字类型比较大小不一致.比如33和111,字符串类型认为前者大.
        vector<long> vecIntNum;
        for (int j = 0; j < vecstrNum.size(); j++)
        {
            long num = 0;
            for (int i = 0; i < vecstrNum[j].size(); i++)
            {
                num += pow(10, vecstrNum[j].size() - i - 1)*(vecstrNum[j][i] - '0');//注意减去'0'
            }
            vecIntNum.push_back(num);
        }
    
        if (strToUpper(strOrder) == "ASCE")
        {
            sort(vecIntNum.begin(), vecIntNum.end());
            for (int j = 0; j < vecstrNum.size() - 1; j++)
            {
                cout << vecIntNum[j] << ',';
            }
            cout << vecIntNum[vecIntNum.size() - 1];
        }
        else//如果是降序就可以倒序输出.
        {
            sort(vecIntNum.begin(), vecIntNum.end());
            for (int j = vecIntNum.size() - 1; j > 0; j--)
            {
                cout << vecIntNum[j] << ',';
            }
            cout << vecIntNum[0];
        }
    
        return 0;
    }
    新战场:https://blog.csdn.net/Stephen___Qin
  • 相关阅读:
    ET之快递测试法学习感悟20140922
    C#单元测试Nunit小结(20141018)
    oracle数据库导入导出09192255
    Mybatis-Configuration-详解
    Mybatis快速入门
    Ajax的学习笔记(一)
    php curl使用总结(一)
    ETL工具--DataX3.0实战
    SqlServer触发器的理解
    2017年的个人计划
  • 原文地址:https://www.cnblogs.com/Stephen-Qin/p/7932054.html
Copyright © 2020-2023  润新知