ปานกลาง 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Recursive Power

Compute a power recursively by halving the exponent.

Compute base to the power exponent with recursion: x⁰ = 1, and xⁿ = (x^(n/2))² for even n or x × (x^((n−1)/2))² for odd n. This uses far fewer multiplications than a simple loop.

Implement recursive_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 = 3
exponent = 5

ผลลัพธ์ที่คาดหวัง:
expected = 243
อินพุต:
long long base = 3;
long long exponent = 5;

ผลลัพธ์ที่คาดหวัง:
long long expected = 243;
อินพุต:
long base = 3;
long exponent = 5;

ผลลัพธ์ที่คาดหวัง:
long expected = 243;

คำอธิบาย

3⁵ = 3 × (3²)² = 3 × 81 = 243.

ตัวอย่างที่ 2
อินพุต:
base = 7
exponent = 0

ผลลัพธ์ที่คาดหวัง:
expected = 1
อินพุต:
long long base = 7;
long long exponent = 0;

ผลลัพธ์ที่คาดหวัง:
long long expected = 1;
อินพุต:
long base = 7;
long exponent = 0;

ผลลัพธ์ที่คาดหวัง:
long expected = 1;

คำอธิบาย

The base case: anything to the power 0 is 1.

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก