- #define _CRT_SECURE_NO_WARNINGS
-
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
-
- int Comparer(char * strLeft, char * strRight)
- {
- char * cmpLeft = strLeft;
- char * cmpRight = strRight;
- for (;; ++cmpLeft, ++cmpRight)
- {
- char chLeft = *cmpLeft;
- char chRight = *cmpRight;
-
- if (!chLeft && !chRight)
- return 0;
-
- if (!chLeft)
- {
- cmpLeft = strRight;
- chLeft = *cmpLeft;
- }
-
- if (!chRight)
- {
- cmpRight = strLeft;
- chRight = *cmpRight;
- }
-
- if (chLeft == chRight)
- continue;
- else
- return chLeft > chRight;
- }
- return 0;
- }
-
- void QS_Swap(char ** left, char ** right)
- {
- char * tmp = *left;
- *left = *right;
- *right = tmp;
- }
-
- int QS_Partition(char ** arr, int lo, int hi)
- {
- char * pivot = arr[hi];
- int i = lo;
-
- for (int j = lo; j < hi; ++j)
- {
- if (Comparer(arr[j], pivot))
- {
- QS_Swap(&arr[i], &arr[j]);
- ++i;
- }
- }
- QS_Swap(&arr[i], &arr[hi]);
- return i;
- }
-
- void QS_Sort(char ** arr, int lo, int hi)
- {
- if (lo < hi)
- {
- int p = QS_Partition(arr, lo, hi);
- QS_Sort(arr, lo, p - 1);
- QS_Sort(arr, p + 1, hi);
- }
- }
-
- void QSort(char ** arr, int len)
- {
- QS_Sort(arr, 0, len - 1);
- }
-
- char * largestNumber(int * nums, int numsSize)
- {
- char * strBuf = (char*)malloc(numsSize * 16);
- char ** strNums = (char**)malloc(sizeof(char*) * numsSize);
- int szTotalLen = 0;
- for (int i = 0; i < numsSize; ++i)
- {
- //char * strItem = _itoa(pNums[i], strBuf, 10);
- sprintf(strBuf, "%d", nums[i]);
- char * strItem = strBuf;
-
- int slen = strlen(strItem);
- szTotalLen += slen;
-
- strBuf += slen + 1;
- strNums[i] = strItem;
- }
-
- QSort(strNums, numsSize);
-
- strBuf = (char*)malloc(szTotalLen + 1);
- char * strRes = strBuf;
- for (int i = 0; i < numsSize; ++i)
- {
- char * strItem = strNums[i];
- strcpy(strBuf, strItem);
- strBuf += strlen(strItem);
- }
-
- while (*strRes == '0' && *(strRes + 1))
- ++strRes;
-
- return strRes;
- }
-
- int main()
- {
- int nums[] = { 0, 0, 1 };
- char *s = largestNumber(nums, sizeof(nums) / sizeof(nums[0]));
- printf("%s", s);
-
- return 0;
- }
复制代码
|