• 把数组排成最小的数


    bool com( const string& a, const string& b )
    {
        string A;
        A.append( a );
        A.append( b );
        string B;
        B.append( b );
        B.append( a );
        
        int i = 0;
        while( A[i] == B[i] )
        {
            i++;
        }
    
        return A[i] < B[i];
    }
    
    string PrintMinNumber(vector<int> numbers)
    {
        string res = "";
        vector< string > snumbers;
        char num[ 10 ];
        string snum;
        for( int i = 0; i < numbers.size(); i++ )
        {
            sprintf( num, "%d", numbers[i] );
            snum = num; //snum.length()为实际字符长度
            snumbers.push_back( snum );
        }
    
        //默认用string的compare函数,字符串比较, "3" < "32" < "321",但与题不符
        //sort默认升序排序
        //sort( snumbers.begin(), snumbers.end() );
    
        sort( snumbers.begin(), snumbers.end(), com );
    
        for( int i = 0; i < snumbers.size(); i++ )
        {
            //cout << snumbers[i];
            res += snumbers[i];
        }
            //cout << endl;
        return res;
    }
  • 相关阅读:
    JavaScript概述
    JavaScript概述
    python语法基础
    python 网络编程
    python 日志模块
    python 异常处理
    python tricks
    记录_省赛(一)
    异或加密算法
    三目条件运算符
  • 原文地址:https://www.cnblogs.com/wangzhiyi/p/6863836.html
Copyright © 2020-2023  润新知