fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #define ll long long
  5.  
  6. int cmp(const void *a, const void * b){
  7. int *p1 = (int*)a;
  8. int *p2 = (int*)b;
  9. return *p1 - *p2;
  10. }
  11.  
  12. int newnum(int n){ //tra ve so moi lon nhat co cung do dai newnum(-7310) - > -1073
  13. int soam = n < 0; //so am = 1 khi n <0;
  14. int a[10] = {0}; //bieu dien cac chu so
  15. int scs = 0; //tinh so chu so cua n
  16. if(soam){
  17. n *= -1;
  18. }
  19. while(n){
  20. a[scs++] = n % 10;
  21. n /= 10;
  22. }
  23. qsort(a, scs, sizeof(int), cmp);
  24. int res = 0;
  25. if(soam){
  26. int p = 0;
  27. while(a[p] == 0) p++;
  28. res = res * 10 + a[p];
  29. for(int i = 0; i < p; i++) res = res * 10;
  30. p++;
  31. for(p; p < scs; p++) res = res * 10 + a[p];
  32. return -1 * res;
  33. }else{
  34. //[0,1,2,3] -> 3210 lap tu scs -> 0;
  35. for(int p = scs-1; p >=0; p--) res = res * 10 + a[p];
  36. return res;
  37. }
  38. }
  39.  
  40.  
  41. void solve(){
  42. int n; scanf("%d", &n);
  43. int a[n];
  44. for(int i=0; i <n; i++){
  45. scanf("%d", a + i);
  46. a[i] = newnum(a[i]);
  47. }
  48. qsort(a, n, sizeof(int), cmp);
  49. for(int i=n-1; i >=0; i--){
  50. printf("%d ", a[i]);
  51. }
  52. }
  53.  
  54. int main(){
  55. int typetest = 0;
  56. if(typetest){
  57. int t; scanf("%d", &t);
  58. while(t--){
  59. solve();
  60. }
  61. }else solve();
  62. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty