Problème
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.
Contraintes
1 ≤ n ≤ 10¹⁸
Exemples
Entrée :
n = 352
Sortie attendue :
expected = 3
Entrée :
long long n = 352;
Sortie attendue :
long long expected = 3;
Entrée :
long n = 352;
Sortie attendue :
long expected = 3;
Explication
352 has three digits.
Entrée :
n = 7
Sortie attendue :
expected = 1
Entrée :
long long n = 7;
Sortie attendue :
long long expected = 1;
Entrée :
long n = 7;
Sortie attendue :
long expected = 1;
Explication
A single digit.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
You only need to know how many times you can remove a digit before nothing remains.
Indice 2
A loop with division by 10 or a string length both work well.
Indice 3
Don’t forget that any positive single-digit number still has exactly one digit.
Complexité attendue
O(d) time, O(1) extra space