#include "iostream"
using namespace std;
const int gMaxLength=10;
char* gCombine1 = new char[gMaxLength * 2 + 1];
char* gCombine2 = new char[gMaxLength * 2 + 1];
int compare(const void* strNum1, const void* strNum2)
{
strcpy_s(gCombine1,sizeof(gCombine1), *(const char**)strNum1);
strcat_s(gCombine1, sizeof(gCombine1)+sizeof(strNum2), *(const char**)strNum2);
strcpy_s(gCombine2, sizeof(gCombine2), *(const char**)strNum2);
strcat_s(gCombine2, sizeof(gCombine2)+sizeof(strNum1), *(const char**)strNum1);
return strcmp(gCombine1, gCombine2);
}
void printMinNumber(int *number,int length)
{
if (number == NULL || length <= 0)
return;
char **strNumber = (char**)(new int[gMaxLength]);
for (int i = 0; i < length;i++)
{
strNumber[i] = new char[gMaxLength + 1];
sprintf_s(strNumber[i],gMaxLength+1, "%d", number[i]);
}
qsort(strNumber, length, sizeof(char*), compare);
for (int i = 0; i < length; i++)
cout << strNumber[i];
cout << endl;
for (int i = 0; i < length;i++)
{
delete[] strNumber[i];
}
delete[] strNumber;
}
int main()
{
int a[] = { 3,2,1 };
printMinNumber(a, 3);
system("pause");
return 0;
}
剑指offer面试题33:把数组排成最小的数
最新推荐文章于 2021-04-17 21:51:46 发布