- Notifications
You must be signed in to change notification settings - Fork 117
/
Copy path179.c
65 lines (52 loc) · 1.51 KB
/
179.c
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
60
61
62
63
64
65
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#defineN 12 /* max number of digits in 10-base integer */
intcompare(inta, intb) {
charstra[N] = { 0 };
charstrb[N] = { 0 };
sprintf(stra, "%d", a);
sprintf(strb, "%d", b);
charaplusb[N+N] = { 0 };
charbplusa[N+N] = { 0 };
strcpy(aplusb, stra);
strcat(aplusb, strb);
strcpy(bplusa, strb);
strcat(bplusa, stra);
returnstrcmp(aplusb, bplusa);
}
voidquicksort(int*nums, intleft, intright) {
if (left>right) return;
inti=left;
intj=right;
intpivot=nums[left];
while (i<j) {
while (i<j&&compare(nums[j], pivot) <= 0) j--;
nums[i] =nums[j];
while (i<j&&compare(nums[i], pivot) >= 0) i++;
nums[j] =nums[i];
}
nums[i] =pivot;
quicksort(nums, left, i-1);
quicksort(nums, i+1, right);
}
char*largestNumber(int*nums, intnumsSize) {
quicksort(nums, 0, numsSize-1);
char*ans= (char*)calloc(numsSize*N, sizeof(char));
inti;
for (i=0; i<numsSize; i++) {
charbuf[N] = { 0 };
sprintf(buf, "%d", nums[i]);
strcat(ans, buf);
}
/* skip leading zeros */
while (*ans=='0'&&*(ans+1) =='0') ans++;
returnans;
}
intmain() {
intnums0[] = { 3, 30, 34, 5, 9 };
printf("%s\n", largestNumber(nums0, sizeof(nums0) / sizeof(nums0[0])));
intnums1[] = { 0, 0 };
printf("%s\n", largestNumber(nums1, sizeof(nums1) / sizeof(nums1[0])));
return0;
}