Advertisement
Guest User

Untitled

a guest
Aug 16th, 2021
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. // Your task is to make a function that can take any non-negative integer as an argument and return it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number.
  2. // Examples:
  3.  
  4. // Input: 42145 Output: 54421
  5.  
  6. // Input: 145263 Output: 654321
  7.  
  8. // Input: 123456789 Output: 987654321
  9.  
  10. #include <inttypes.h>
  11.  
  12. const char triamgle[101] = "o........."
  13.                            "oo........"
  14.                            "ooo......."
  15.                            "oooo......"
  16.                            "ooooo....."
  17.                            "oooooo...."
  18.                            "ooooooo..."
  19.                            "oooooooo.."
  20.                            "ooooooooo."
  21.                            "oooooooooo";
  22.  
  23. uint64_t descendingOrder(uint64_t n)
  24. {
  25.     uint64_t a, p, r;
  26.  
  27.     a = n;
  28.     p = 1;
  29.     r = 0;
  30.  
  31.     for(; a / 10; r += a % 10 * p, p *= 10, a /= 10)
  32.         if(triamgle[a % 100] == '.')
  33.             return descendingOrder(r + p * ((a / 100 * 100) + a % 10 * 10 + a / 10 % 10));
  34.     return n;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement