Problème
Body mass index (BMI) is weight in kilograms divided by the square of height in meters.
Implement body_mass_index(weight_kg, height_m).
Input
weight_kg: Weight in kilograms. height_m: Height in meters.
Output
Return weight_kg / (height_m × height_m).
Contraintes
1 ≤ weight_kg ≤ 500; 0.5 ≤ height_m ≤ 2.5
Exemples
Entrée :
height_m = 1.8
weight_kg = 72.0
Sortie attendue :
expected = 22.222222
Entrée :
double height_m = 1.8;
double weight_kg = 72.0;
Sortie attendue :
double expected = 22.222222;
Entrée :
double height_m = 1.8;
double weight_kg = 72.0;
Sortie attendue :
double expected = 22.222222;
Explication
1.8 × 1.8 = 3.24 and 72 / 3.24 ≈ 22.2222.
Entrée :
height_m = 1.0
weight_kg = 50.0
Sortie attendue :
expected = 50.0
Entrée :
double height_m = 1.0;
double weight_kg = 50.0;
Sortie attendue :
double expected = 50.0;
Entrée :
double height_m = 1.0;
double weight_kg = 50.0;
Sortie attendue :
double expected = 50.0;
Explication
With a height of 1 meter the BMI equals the weight: 50.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
Le BMI compare le poids à la taille au carré, donc le terme de taille compte deux fois.
Indice 2
Mets la taille au carré d’abord, ou fais attention aux parenthèses lors de la division.
Indice 3
Utilise une division décimale, car la réponse n’est généralement pas un entier.
Complexité attendue
temps O(1), espace supplémentaire O(1)
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.