标签:
题目:输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这3个数字能排成的最小数字321323.
1 const int g_MaxNumberLength=10; 2 char* g_StrCombine1=new char[g_MaxNumberLength*2+1]; 3 char* g_StrCombine2=new char[g_MaxNumberLength*2+1]; 4 5 int compare(const void* strNumber1,const void* strNumber2) 6 { 7 strcpy(g_StrCombine1,*(const char**)strNumber1); 8 strcat(g_StrCombine1,*(const char**)strNumber2); 9 10 strcpy(g_StrCombine2,*(const char**)strNumber2); 11 strcat(g_StrCombine2,*(const char**)strNumber1); 12 return strcmp(g_StrCombine1,g_StrCombine2); 13 } 14 15 void PrintMinNumber(int* numbers,int length) 16 { 17 if(numbers==NULL||length<=0) 18 return; 19 char** strNumbers=(char**)(new int[length]); 20 for(int i=0;i<length;++i) 21 { 22 strNumbers[i]=new char[g_MaxNumberLength+1]; 23 sprintf(strNumbers[i],"%d",numbers[i]); 24 } 25 qsort(strNumbers,length,sizeof(char*),compare); 26 for(i=0;i<length;++i) 27 printf("%s",strNumbers[i]); 28 printf("\n"); 29 for(i=0;i<length;++i) 30 delete [] strNumbers[i]; 31 delete [] strNumbers; 32 }
标签:
原文地址:http://www.cnblogs.com/wxdjss/p/5484309.html