すべてのチャレンジ
中級 1件解決済み 正答率100.0%

Raise a Number to a Fractional Power

Raise a positive real number to a rational exponent.

このチャレンジで学べること

  • Turn a rational exponent into a root and a power
  • Use floating-point math safely
  • Handle reduced and negative exponents

問題

翻訳を表示

The exponent is numerator / denominator. Raise base_value to that exponent and return the real-valued result. For example, an exponent of 1/3 gives the positive cube root.

Implement real_to_rational_power(base_value, numerator, denominator).

Input

base_value: The positive base.

numerator: The exponent numerator.

denominator: The exponent denominator.

Output

Return base_value raised to numerator / denominator.

制約

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

例

例 1
入力:
base_value = 8.0
numerator = 1
denominator = 3

期待される出力:
expected = 2.0
入力:
double base_value = 8.0;
long long numerator = 1;
long long denominator = 3;

期待される出力:
double expected = 2.0;
入力:
double base_value = 8.0;
long numerator = 1;
long denominator = 3;

期待される出力:
double expected = 2.0;

解説

2 × 2 × 2 = 8, so the cube root is 2.

例 2
入力:
base_value = 16.0
numerator = -1
denominator = 2

期待される出力:
expected = 0.25
入力:
double base_value = 16.0;
long long numerator = -1;
long long denominator = 2;

期待される出力:
double expected = 0.25;
入力:
double base_value = 16.0;
long numerator = -1;
long denominator = 2;

期待される出力:
double expected = 0.25;

解説

The square root of 16 is 4, and the negative exponent takes its reciprocal, 1/4.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

First turn the exponent fraction into a single real exponent.

ヒント 2

Then raise the positive base using a real-power operation.

ヒント 3

Make sure negative exponents produce reciprocals, and avoid mistakes with fractional roots.

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 ННикита Python 833 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意:下に1件の全解答あり まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。