問題
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
例
入力:
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.
入力:
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
The exponent tells you how many times to keep multiplying the base into an accumulating result.
ヒント 2
Start from the neutral value for multiplication so the zero-exponent case works naturally.
ヒント 3
Watch for the edge case where the exponent is zero, since no looping should still give a valid answer.
目安の計算量
O(exponent) time, O(1) extra space
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。