Условие
Count how many decimal digits n has when written normally, without leading zeros.
Implement count_digits(n).
Input
n: A positive integer.
Output
Return the number of digits.
Ограничения
1 ≤ n ≤ 10¹⁸
Примеры
Ввод:
n = 352
Ожидаемый вывод:
expected = 3
Ввод:
long long n = 352;
Ожидаемый вывод:
long long expected = 3;
Ввод:
long n = 352;
Ожидаемый вывод:
long expected = 3;
Пояснение
352 has three digits.
Ввод:
n = 7
Ожидаемый вывод:
expected = 1
Ввод:
long long n = 7;
Ожидаемый вывод:
long long expected = 1;
Ввод:
long n = 7;
Ожидаемый вывод:
long expected = 1;
Пояснение
A single digit.
Как подступиться
Сначала попробуйте сами, затем открывайте подсказки по одной.
Подсказка 1
You only need to know how many times you can remove a digit before nothing remains.
Подсказка 2
A loop with division by 10 or a string length both work well.
Подсказка 3
Don’t forget that any positive single-digit number still has exactly one digit.
Ожидаемая сложность
O(d) time, O(1) extra space