すべてのチャレンジ
簡単 2件解決済み 正答率40.0%

Power by Repeated Multiplication

Raise a number to a power with a loop.

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

  • ループで掛け算を繰り返す
  • 指数が 0 の場合を扱う
  • 組み込みの累乗と比べる

問題

Compute base to the power exponent by multiplying base by itself exponent times in a loop. Any number to the power 0 is 1.

Implement power(base, exponent).

Input

base: The base. exponent: The exponent (zero or positive).

Output

Return base raised to exponent.

制約

-10 ≤ base ≤ 10; 0 ≤ exponent ≤ 18

例

例 1
入力:
base = 2
exponent = 10

期待される出力:
expected = 1024
入力:
long long base = 2;
long long exponent = 10;

期待される出力:
long long expected = 1024;
入力:
long base = 2;
long exponent = 10;

期待される出力:
long expected = 1024;

解説

2 multiplied by itself 10 times is 1024.

例 2
入力:
base = 5
exponent = 0

期待される出力:
expected = 1
入力:
long long base = 5;
long long exponent = 0;

期待される出力:
long long expected = 1;
入力:
long base = 5;
long exponent = 0;

期待される出力:
long expected = 1;

解説

Anything to the power 0 is 1.

解き方のヒント

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

ヒント 1

指数は、基数を何回掛け続けるかを表す。

ヒント 2

乗算の単位元から始めると、指数0のケースも自然に処理できる。

ヒント 3

指数が0のときはループ不要でも正しい答えになるので注意する。

目安の計算量

時間 O(exponent)、追加メモリ O(1)

参考資料

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

ランキング

全体ランキング

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

# コーダー 言語 時間 メモリ AI 評価 解答
1 HJhettory Jurash Python 965 ms - 4/5 コードを見る
2 LMLaura Mahlow Python 1,036 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意:下に2件の全解答あり まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。