#include<iostream>
#include <iomanip>
#define abs(x) (x>0?(x):(-x))
using namespace std;
double is_in(int g1, int g2, int t) { // 判断差是否在公差范围内
if (abs(g1 - g2) <= t) {
return (double) (g1 + g2) / 2;
} else {
return -1;
}
}
double close(int g3, int g1, int g2) { // 找到与g3最接近的那个数并求其平均值
if (abs(g3 - g1) >= abs(g3 - g2)) {
return (double) (g3 + g2) / 2;
} else {
return (double) (g3 + g1) / 2;
}
}
int main(void) {
int p, t, g1, g2, g3, gj;
::scanf("%d%d%d%d%d", &p, &t, &g1, &g2, &g3, &gj);
if (is_in(g1, g2, t))
{
cout << fixed << setprecision(1) << is_in(g1, g2, t);
}
else if (is_in(g1, g3, t) && is_in(g2, g3, t))
{
cout << fixed << setprecision(1) << max(max(g1, g2), g3);
}
else if (is_in(g1, g3, t) || is_in(g2, g3, t))
{
cout << fixed << setprecision(1) << close(g3, g1, g2);
}
else
{
cout << fixed << setprecision(1) << (double) gj;
}
}
要注意要求的输出值是保留了小数点后一位,我用的cout输出,保留小数点后一位的话语法是
cout << fixed << setprecision(1) << (double) gj;
如果不用fixed,那么保留的就是有效数字1位,就不是从小数点后开始算起的了