#13956. [GESP202609 二级 C++] 第 15 题

[GESP202609 二级 C++] 第 15 题

小北的身高是 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;

{{ select(1) }}

  • if (h   < best_height)
         best_height   =  h;
    
  • if (h   > best_height)
         best_height   =  h;
    
  • if (h   > target)
         best_height   =  h;
    
  • if (h   >= best_height)
         best_height   =  h;