Tous les défis
Facile 4 résolus 100.0 % de réussite

Body Mass Index

Compute BMI from weight and height.

Ce que vous allez apprendre

  • Square a value and divide
  • Work with decimals
  • Translate a formula into code

Problème

Afficher la traduction

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

Exemple 1
Entrée :
weight_kg = 72.0
height_m = 1.8

Sortie attendue :
expected = 22.222222
Entrée :
double weight_kg = 72.0;
double height_m = 1.8;

Sortie attendue :
double expected = 22.222222;
Entrée :
double weight_kg = 72.0;
double height_m = 1.8;

Sortie attendue :
double expected = 22.222222;

Explication

1.8 × 1.8 = 3.24 and 72 / 3.24 ≈ 22.2222.

Exemple 2
Entrée :
weight_kg = 50.0
height_m = 1.0

Sortie attendue :
expected = 50.0
Entrée :
double weight_kg = 50.0;
double height_m = 1.0;

Sortie attendue :
double expected = 50.0;
Entrée :
double weight_kg = 50.0;
double height_m = 1.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

BMI compares weight to height squared, so the height term matters twice.

Indice 2

Square the height first or be careful with parentheses when dividing.

Indice 3

Use decimal division, because the answer is usually not an integer.

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

# Codeur Langage Temps Mémoire Note de l'IA Solution
1 ДЗДавид Зайдулин Python 843 ms - 4/5 Voir le code
2 LMLaura Mahlow Python 860 ms - 5/5 Voir le code
3 PKPornchai Kaewkrabil Go 1,177 ms - 5/5 Voir le code
4 GTGarlic The Destroyer Rust 1,238 ms - 5/5 Voir le code

Apprenez des autres

Solutions de la communauté

Attention spoiler : 4 solutions complètes ci-dessous Essayez d'abord de le résoudre vous-même. Vous pouvez toujours jeter un œil au code des autres développeurs et aux avis IA. Connectez-vous et résolvez le défi pour rejoindre la discussion.