另外,由于把数字m和n拼接起来得到的mn和nm,它们所含有的数字的个数肯定是相同的。因此比较它们的大小只需要按照字符串大小的比较规则就可以了。
基于这个思路,我们可以写出下面的代码:
// Maxinum int number has 10 digits in decimal system
const int g_MaxNumberLength = 10;
// String buffers to combine two numbers
char* g_StrCombine1 = new char[g_MaxNumberLength * 2 + 1];
char* g_StrCombine2 = new char[g_MaxNumberLength * 2 + 1];
// Given an array, print the minimum number
// by combining all numbers in the array
void PrintMinNumber(int* numbers, int length)
{
if(numbers == NULL || length <= 0)
return;
// Convert all numbers as strings
char** strNumbers = (char**)(new int[length]);
for(int i = 0; i < length; ++i)
{
strNumbers[i] = new char[g_MaxNumberLength + 1];
sprintf(strNumbers[i], "%d", numbers[i]);