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

Armstrong Number

Check whether a number equals the sum of its digits raised to the digit count.

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

  • Count the digits of a number
  • Raise each digit to the digit count and add them up
  • Compare the result with the original number

問題

翻訳を表示

A number with k digits is an Armstrong number if the sum of each digit raised to the power k equals the number. For example 153 = 1³ + 5³ + 3³.

Implement is_armstrong(n).

Input

n: A non-negative integer.

Output

Return true if n is an Armstrong number.

制約

0 ≤ n ≤ 10¹⁸

例

例 1
入力:
n = 153

期待される出力:
expected = True
入力:
long long n = 153;

期待される出力:
bool expected = true;
入力:
long n = 153;

期待される出力:
bool expected = true;

解説

1³ + 5³ + 3³ = 1 + 125 + 27 = 153.

例 2
入力:
n = 10

期待される出力:
expected = False
入力:
long long n = 10;

期待される出力:
bool expected = false;
入力:
long n = 10;

期待される出力:
bool expected = false;

解説

1² + 0² = 1, which is not 10.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Count how many digits the number has before summing powers.

ヒント 2

Compare the original number with the sum of each digit raised to that digit count.

ヒント 3

Remember that 0 is a special one-digit case.

目安の計算量

O(k) time, O(1) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

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