簡単 0件解決済み 正答率0%

Count Digits

Count the decimal digits of a positive integer.

このチャレンジで学べること

  • Count how many times a number can be divided by 10
  • Use a counter inside a while loop
  • Treat single-digit numbers as an edge case

問題

翻訳を表示

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

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。