这些数所能拼凑出的最大数字是 9 5 34 3 30。- // 2018-09-29
-
- # include <stdio.h>
- # include <stdlib.h>
-
- typedef long long i64;
- typedef struct { int tail; int val; } Num;
-
- void sort(int*, int*, int);
- int cmp(const void*, const void*);
-
- int main (){
- int test[] = { 3, 30, 34, 5, 9 };
- int len = sizeof(test) / sizeof(*test);
- int sorted[len];
-
- sort (test, sorted, len);
- for (int i = 0; i < len; i++)
- printf ("%d ", sorted[i]);
-
- }
-
- # define join(A, B) (A->val * (i64)B->tail + B->val)
- int cmp(const void* A, const void* B){
- Num* a = (Num*) A;
- Num* b = (Num*) B;
- i64 ab = join (a, b);
- i64 ba = join (b, a);
- if (ab == ba) return 0;
- return ab > ba ? -1 : 1;
- }
-
- void sort(int* test, int* sorted, int len){
- Num n[len];
-
- for (int i = 0; i < len; i++){
- int tail = 1;
- int val = test[i];
-
- while (val) tail *= 10, val /= 10;
-
- n[i].tail = tail;
- n[i].val = test[i];
- }
-
- qsort(n, len, sizeof(Num), cmp);
- for (int i = 0; i < len; i++) sorted[i] = n[i].val;
- }
-
复制代码
|