Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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.
- // Examples:
- // Input: 42145 Output: 54421
- // Input: 145263 Output: 654321
- // Input: 123456789 Output: 987654321
- #include <inttypes.h>
- const char triamgle[101] = "o........."
- "oo........"
- "ooo......."
- "oooo......"
- "ooooo....."
- "oooooo...."
- "ooooooo..."
- "oooooooo.."
- "ooooooooo."
- "oooooooooo";
- uint64_t descendingOrder(uint64_t n)
- {
- uint64_t a, p, r;
- a = n;
- p = 1;
- r = 0;
- for(; a / 10; r += a % 10 * p, p *= 10, a /= 10)
- if(triamgle[a % 100] == '.')
- return descendingOrder(r + p * ((a / 100 * 100) + a % 10 * 10 + a / 10 % 10));
- return n;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement