Tất cả thử thách
Trung bình 1 đã giải Tỷ lệ chấp nhận 100.0%

Raise a Fraction to a Real Power

Raise a positive fraction to a real-valued exponent.

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

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

Ví dụ

Ví dụ 1
Đầu vào:
numerator = 9
denominator = 4
power = 0.5

Kết quả mong đợi:
expected = 1.5
Đầu vào:
long long numerator = 9;
long long denominator = 4;
double power = 0.5;

Kết quả mong đợi:
double expected = 1.5;
Đầu vào:
long numerator = 9;
long denominator = 4;
double power = 0.5;

Kết quả mong đợi:
double expected = 1.5;

Giải thích

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

Ví dụ 2
Đầu vào:
numerator = 2
denominator = 5
power = -1.0

Kết quả mong đợi:
expected = 2.5
Đầu vào:
long long numerator = 2;
long long denominator = 5;
double power = -1.0;

Kết quả mong đợi:
double expected = 2.5;
Đầu vào:
long numerator = 2;
long denominator = 5;
double power = -1.0;

Kết quả mong đợi:
double expected = 2.5;

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 ННикита Python 788 ms - 5/5 Xem code

Học hỏi từ người khác

Giải pháp cộng đồng

Cảnh báo: Có 1 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem mã của người khác và đánh giá AI. Đăng nhập và giải thử thách để tham gia thảo luận.