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

Disarium Number

Check whether a number equals the sum of its digits raised to their positions.

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

  • Read digits from left to right
  • Raise a digit to its position with integer arithmetic
  • Add powers without overflow

問題

翻訳を表示

Number the digits of n from the left starting at 1, raise each digit to the power of its position, and add the results. If the total is n itself, the number is a disarium number. For example, 175 gives 1¹ + 7² + 5³ = 1 + 49 + 125 = 175.

Implement disarium_number(n).

Input

n: The number to test.

Output

Return true if n is a disarium number, otherwise false. Every single-digit number is a disarium number.

制約

1 ≤ n ≤ 10^9

例

例 1
入力:
n = 89

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

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

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

解説

8¹ + 9² = 8 + 81 = 89, so the answer is true.

例 2
入力:
n = 98

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

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

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

解説

9¹ + 8² = 9 + 64 = 73, not 98, so the answer is false.

解き方のヒント

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

ヒント 1

Positions count from the left, so either turn n into a string or count the digits first.

ヒント 2

Use integer multiplication for powers instead of a floating point function.

ヒント 3

The largest term is 9^10, which fits easily in 64 bits.

目安の計算量

O(d^2) time for d digits, O(1) extra space

参考資料

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

ランキング

全体ランキング

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

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

他の人の解答から学ぶ

コミュニティの解答

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