Problem 6: How Many Digits?
https://open.kattis.com/problems/howmanydigits
The number of digits of an integer can be calculated by the following formula:
\text{# of digits in }x =
\begin{cases}
\log_{10} (x)+1 & : x \text{ is a power of }10 \\
\lceil \log_{10} (x) \rceil & : \text{ otherwise}
\end{cases}
is the ceiling function, which rounds a number up to the closest larger integer.
Another important property of logs: .
is only a power of 10 if or , so handle those cases separately. Otherwise:
Store the sums in a vector, and output the ceiling of that sum for each test case.
C++ has functions ceil(x)
and log10(x)
that can be used for calculations. Be careful that ceil()
returns a double, so cast to int
or long long
before printing.
Last updated