Tutte le sfide
Media 1 risolta 100.0% di accettazione

Raise a Fraction to a Real Power

Raise a positive fraction to a real-valued exponent.

Cosa imparerai

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

Problema

Mostra traduzione

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.

Vincoli

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

Esempi

Esempio 1
Input:
numerator = 9
denominator = 4
power = 0.5

Output atteso:
expected = 1.5
Input:
long long numerator = 9;
long long denominator = 4;
double power = 0.5;

Output atteso:
double expected = 1.5;
Input:
long numerator = 9;
long denominator = 4;
double power = 0.5;

Output atteso:
double expected = 1.5;

Spiegazione

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

Esempio 2
Input:
numerator = 2
denominator = 5
power = -1.0

Output atteso:
expected = 2.5
Input:
long long numerator = 2;
long long denominator = 5;
double power = -1.0;

Output atteso:
double expected = 2.5;
Input:
long numerator = 2;
long denominator = 5;
double power = -1.0;

Output atteso:
double expected = 2.5;

Spiegazione

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

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

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

Suggerimento 2

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

Suggerimento 3

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

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 ННикита Python 788 ms - 5/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 1 soluzione completa sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le recensioni AI. Accedi e risolvi la sfida per partecipare alla discussione.