Đề bài
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).
Ràng buộc
1 ≤ weight_kg ≤ 500; 0.5 ≤ height_m ≤ 2.5
Ví dụ
Đầu vào:
weight_kg = 72.0
height_m = 1.8
Kết quả mong đợi:
expected = 22.222222
Đầu vào:
double weight_kg = 72.0;
double height_m = 1.8;
Kết quả mong đợi:
double expected = 22.222222;
Đầu vào:
double weight_kg = 72.0;
double height_m = 1.8;
Kết quả mong đợi:
double expected = 22.222222;
Giải thích
1.8 × 1.8 = 3.24 and 72 / 3.24 ≈ 22.2222.
Đầu vào:
weight_kg = 50.0
height_m = 1.0
Kết quả mong đợi:
expected = 50.0
Đầu vào:
double weight_kg = 50.0;
double height_m = 1.0;
Kết quả mong đợi:
double expected = 50.0;
Đầu vào:
double weight_kg = 50.0;
double height_m = 1.0;
Kết quả mong đợi:
double expected = 50.0;
Giải thích
With a height of 1 meter the BMI equals the weight: 50.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
BMI compares weight to height squared, so the height term matters twice.
Gợi ý 2
Square the height first or be careful with parentheses when dividing.
Gợi ý 3
Use decimal division, because the answer is usually not an integer.
Độ 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.