1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include <iostream> #include <cstdlib> #include <string> #include <cstdio> using namespace std; const int g_MaxNumberLength = 10; char *g_StrCombine1 = new char[g_MaxNumberLength * 2 + 1]; char *g_StrCombine2 = new char[g_MaxNumberLength * 2 + 1]; int compare(const void *strNumber1, const void *strNumber2) { strcpy(g_StrCombine1, *(const char **)strNumber1); strcat(g_StrCombine1, *(const char **)strNumber2); strcpy(g_StrCombine2, *(const char **)strNumber2); strcat(g_StrCombine2, *(const char **)strNumber1); return strcmp(g_StrCombine1, g_StrCombine2); } void PrintMinNumber(int *numbers, int length) { if (numbers == NULL || length < 0) { return ; } 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]); } qsort(strNumbers, length, sizeof(char *), compare); for (int i = 0; i < length; ++i) { printf("%s", strNumbers[i]); } cout << endl; for (int i = 0; i < length; ++i) { delete[] strNumbers[i]; } delete[] strNumbers; } int main() { int num[] = {3, 32, 321}; int length = sizeof(num) / sizeof(num[0]); PrintMinNumber(num, length); return 0; } |
【面试题033】把数组排成最小的数,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/codemylife/p/3745017.html