簡単 4件解決済み 正答率100.0%

Body Mass Index

Compute BMI from weight and height.

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

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

問題

翻訳を表示

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).

制約

1 ≤ weight_kg ≤ 500; 0.5 ≤ height_m ≤ 2.5

例

例 1
入力:
weight_kg = 72.0
height_m = 1.8

期待される出力:
expected = 22.222222
入力:
double weight_kg = 72.0;
double height_m = 1.8;

期待される出力:
double expected = 22.222222;
入力:
double weight_kg = 72.0;
double height_m = 1.8;

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

解説

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

例 2
入力:
weight_kg = 50.0
height_m = 1.0

期待される出力:
expected = 50.0
入力:
double weight_kg = 50.0;
double height_m = 1.0;

期待される出力:
double expected = 50.0;
入力:
double weight_kg = 50.0;
double height_m = 1.0;

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

解説

With a height of 1 meter the BMI equals the weight: 50.

解き方のヒント

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

ヒント 1

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

ヒント 2

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

ヒント 3

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

目安の計算量

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

参考資料

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

ランキング

全体ランキング

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

# コーダー 言語 時間 メモリ AI 評価 解答
1 ДЗДавид Зайдулин Python 843 ms - 4/5 コードを見る
2 LMLaura Mahlow Python 860 ms - 5/5 コードを見る
3 PKPornchai Kaewkrabil Go 1,177 ms - 5/5 コードを見る
4 GTGarlic The Destroyer Rust 1,238 ms - 5/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

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