I was doing this challenge:
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.
I tried to avoid using sorting so I made this:
#include <math.h>
#include <iostream>
int main()
{
uint64_t a = 1234567889;
uint64_t result = 0;
int stupid_idea[10] = {0};
//This counts the number of digits
while (a > 0){
stupid_idea[a%10] += 1;
a = a/10;
}
//This (should) use the numbers of digits to then order them
for (int i=9;i>=0;i--){
int power = pow(10,stupid_idea[i]);
//This is here so you can see pow (debuggin)
std::cout << power << '\n';
result *= power;
result += i*((power-1)/9);
}
std::cout << result;
return 0;
}
The final result should be:
9887654321
I am actually getting:
9717654321
The problem seems to be pow(10,stupid_idea[i])
which is giving me 99 in a case and I don't know how 10x can be 99.
Compiler version:
win32 gcc 9.2.0
Compile command:
c++ main.cpp
Log:
10
99
10
10
10
10
10
10
10
1
9717654321