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

Raise a Fraction to a Real Power

Raise a positive fraction to a real-valued exponent.

สิ่งที่คุณจะได้เรียนรู้

  • Raise a fraction to a real exponent
  • Work with floating-point results
  • Keep results within tolerance

โจทย์

ดูฉบับแปล

Interpret numerator / denominator as a positive rational number, then raise it to power. This task returns a real number rather than an exact fraction; it covers the real-exponent part of the Rational Numbers reference.

Implement rational_to_real_power(numerator, denominator, power).

Input

numerator: The positive numerator.

denominator: The positive denominator.

power: The real-valued exponent.

Output

Return (numerator / denominator) raised to power.

เงื่อนไข

1 ≤ numerator, denominator ≤ 100. −6 ≤ power ≤ 6, with at most two decimal places. Answers are accepted within absolute or relative error 0.000001.

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
numerator = 9
denominator = 4
power = 0.5

ผลลัพธ์ที่คาดหวัง:
expected = 1.5
อินพุต:
long long numerator = 9;
long long denominator = 4;
double power = 0.5;

ผลลัพธ์ที่คาดหวัง:
double expected = 1.5;
อินพุต:
long numerator = 9;
long denominator = 4;
double power = 0.5;

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

คำอธิบาย

An exponent of 0.5 takes the positive square root: √(9/4) = 3/2 = 1.5.

ตัวอย่างที่ 2
อินพุต:
numerator = 2
denominator = 5
power = -1.0

ผลลัพธ์ที่คาดหวัง:
expected = 2.5
อินพุต:
long long numerator = 2;
long long denominator = 5;
double power = -1.0;

ผลลัพธ์ที่คาดหวัง:
double expected = 2.5;
อินพุต:
long numerator = 2;
long denominator = 5;
double power = -1.0;

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

คำอธิบาย

The exponent −1 takes the reciprocal: 5/2 = 2.5.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Treat the fraction as one positive real value before applying the exponent.

คำใบ้ 2

Use floating-point exponentiation directly, since the answer is approximate.

คำใบ้ 3

Be careful with negative and fractional-looking exponents, and keep enough precision for the tolerance.

ความซับซ้อนที่คาดหวัง

O(1) time, O(1) extra space

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

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

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

# นักโค้ด ภาษา เวลา หน่วยความจำ คะแนนจาก AI คำตอบ
1 ННикита Python 788 ms - 5/5 ดูโค้ด

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

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

สปอยล์: มีคำตอบเต็ม 1 ข้อด้านล่าง ลองแก้ด้วยตัวเองก่อน คุณยังสามารถดูโค้ดของนักพัฒนาคนอื่นและรีวิว AI ได้ เข้าสู่ระบบและแก้โจทย์เพื่อร่วมพูดคุย