题解:洛谷 AT_abc467_a Obesity
【题目来源】
洛谷:AT_abc467_a [ABC467A] Obesity
【题目描述】
The value calculated by the following formula is called \(\mathrm{BMI}[\mathrm{kg}/\mathrm{m}^2]\). (\(\div\) denotes division.)
- \(\text{Weight}[\mathrm{kg}] \div \text{Height}[\mathrm{m}] \div \text{Height}[\mathrm{m}]\)
In Japan, a person whose \(\mathrm{BMI}\) is \(25 \; \mathrm{kg}/\mathrm{m}^2\) or more is considered obese.
Determine whether a person with height \(H[\mathrm{cm}]\) and weight \(W[\mathrm{kg}]\) is considered obese in Japan.
以下公式计算的值称为 \(\mathrm{BMI}[\mathrm{kg}/\mathrm{m}^2]\)。(\(\div\) 表示除法。)
- \(\text{体重}[\mathrm{kg}] \div \text{身高}[\mathrm{m}] \div \text{身高}[\mathrm{m}]\)
在日本,\(\mathrm{BMI}\) 达到 \(25 \; \mathrm{kg}/\mathrm{m}^2\) 或以上的人被视为肥胖。
判断一个身高为 \(H[\mathrm{cm}]\)、体重为 \(W[\mathrm{kg}]\) 的人在日本是否被视为肥胖。
【输入】
The input is given from Standard Input in the following format:
\(H\) \(W\)
【输出】
Output Yes on one line if a person with height \(H[\mathrm{cm}]\) and weight \(W[\mathrm{kg}]\) is considered obese in Japan, and No otherwise.
【输入样例】
180 60
【输出样例】
No
【核心思想】
-
问题分析:给定身高 \(H\)(厘米)和体重 \(W\)(千克),计算 BMI 并判断是否 \(\geq 25\)。BMI 公式为 \(W / (H/100)^2\)。这是一个公式计算 + 条件判断问题,核心在于正确转换单位并避免浮点精度问题。
-
算法选择:
- 整数运算替代浮点:将不等式 \(W / (H/100)^2 \geq 25\) 两边同乘转化为整数比较,避免浮点误差
-
关键步骤:
- 读入数据:读取 \(H, W\)
- 单位转换与判断:
- 身高转换为米:\(H_{m} = H / 100\)
- BMI 公式:\(BMI = W / (H_{m})^2 = W \times 10000 / H^2\)
- 判断 \(W \times 10000 \geq 25 \times H^2\)
- 输出结果:若满足则
Yes,否则No
-
时间/空间复杂度:
- 时间复杂度:\(O(1)\)
- 空间复杂度:\(O(1)\)
-
整数运算防浮点误差的核心思想:
- 不等式变形:\(W / (H/100)^2 \geq 25 \Leftrightarrow W \times 10000 \geq 25 \times H^2\),将除法转化为乘法,完全避免浮点运算
- 精度保证:整数乘法精确无误,而浮点除法可能因精度问题导致边界判断错误(如 BMI 恰好为 \(25\) 时)
- 常数选择:\(10000 = 100^2\),对应厘米到米的平方转换,确保公式等价性
- 适用于基础物理公式计算、浮点精度敏感类入门问题
【算法标签】
模拟
【代码详解】
#include <bits/stdc++.h>
using namespace std;
int h, w; // h: 身高(厘米); w: 体重(千克)
int main()
{
cin >> h >> w; // 读入身高和体重
// 判断 BMI >= 25:w / (h/100)^2 >= 25
// 等价于 w * 10000 >= 25 * h * h(避免浮点运算)
if (w * 10000 >= 25 * h * h)
cout << "Yes" << endl; // BMI >= 25,视为肥胖
else
cout << "No" << endl; // BMI < 25,不视为肥胖
return 0;
}
【运行结果】
180 60
No
浙公网安备 33010602011771号