‹ 上一题 下一题 ›

小北的身高是 1.65 米。班上有 n 个小朋友,小北想和身高最接近自己的人交朋友。如果有多个小朋友与小北的身高差相同,小北想和其中身高较矮的第一个小朋友做朋友。横线处应该填入的是(  )。

int n, i;
float target, h, best_height, min_diff, diff;
cout << "请输入小朋友的人数: ";
cin >> n;
target = 1.65;
best_height = 0.0;
min_diff = 100.0;
for (i = 0; i < n; i++) {
    cout << "请输入第 " << (i + 1) << " 个小朋友的身高: ";
    cin >> h;
    if (h > target)
        diff = h - target;
    else
        diff = target - h;
    if (diff < min_diff) {
        min_diff = diff;
        best_height = h;
    } else if (diff == min_diff) {
        __________________
        __________________
    }
}
cout << "小北的朋友身高为:" << best_height;
  • A. ```cpp
    if (h < best_height)
    best_height = h;
- **B.** ```cpp
if (h > best_height)
    best_height = h;
  • C. ```cpp
    if (h > target)
    best_height = h;
- **D.** ```cpp
if (h >= best_height)
    best_height = h;
未选择
输入
输出

            
答案