問題
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¹⁸
例
例 1
入力:
n = 352
期待される出力:
expected = 3
入力:
long long n = 352;
期待される出力:
long long expected = 3;
入力:
long n = 352;
期待される出力:
long expected = 3;
解説
352 has three digits.
例 2
入力:
n = 7
期待される出力:
expected = 1
入力:
long long n = 7;
期待される出力:
long long expected = 1;
入力:
long n = 7;
期待される出力:
long expected = 1;
解説
A single digit.
解き方のヒント
まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。
ヒント 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