面试题 33 把数组排成最小的数
const int g_maxlen = 10; char * g_strCombine1 = new char[g_maxlen*2 +1]; char * g_strCombine2 = new char[g_maxlen*2 +1]; int compare(const void *str1, const void *str2) { strcpy(g_strCombine1, *(const char **)str1); strcat(g_strCombine1, *(const char **)str2);// important strcpy(g_strCombine2, *(const char **)str2); strcat(g_strCombine2, *(const char **)str1); return strcmp(g_strCombine1, g_strCombine2); } void printMinNum(int number[], int len) { if(number == NULL || len < 1) return ; char **strNum = (char **)(new int[len]) ;// 指针的大小是int的大小 for(int i = 0; i< len ; i++) { strNum[i] = new char[g_maxlen +1]; sprintf(strNum[i], "%d", number[i]); // important } qsort(strNum,len, sizeof(char*), compare);// important for(int i = 0; i < len; ++i) printf("%s", strNum[i]); for(int i = 0; i< len; ++i) delete [] strNum[i]; delete [] strNum; }
--------------------------------------------------------------------天道酬勤!