Условие
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
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. Следуйте условию и ограничениям выше.