剑指Offer31 把数组排成最小的数

时间:2021-02-22 15:16:54
 /*************************************************************************
> File Name: 31_SortArrayMin.cpp
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: 2016年09月02日 星期五 11时10分42秒
************************************************************************/ #include <stdio.h>
#include <string>
#include <bits/stdc++.h> using namespace std; string itos(int x)
{
return (x> ? itos(x/) : "") + char(x% + '');
} // qsort比较函数
int cmp(const void *a, const void *b)
{
return *(int*)a - *(int*)b;
} // sort比较函数
bool compare(int a, int b)
{
return (itos(a) + itos(b)) < (itos(b) + itos(a));
} string PrintMinNumber(int* nums, int length)
{
string s = "";
if (nums==NULL || length<=)
return s;
// sort(nums, nums+length, compare);
qsort(nums, length, sizeof(int), cmp); for (int i = ; i < length; ++i)
s += itos(nums[i]);
cout << s << endl;
return s;
} int main()
{
int nums[] = {, , };
int length = ;
PrintMinNumber(nums, length); return ;
}