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;
}

 

posted on 2017-05-16 21:38  chenguang9239  阅读(168)  评论(0编辑  收藏  举报