按照题目模拟即可

· · 题解

if 判断,对于每个范围输出对应的即可,注意保留三位小数,我用的是 coutsetprecision(),当然 printf 也可以。

代码:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    double x;
    cin >> x;
    if (x >= 0 && x < 5)
    {
        cout << fixed << setprecision(3) << -x + 2.5 << endl;
    }
    else if (x >= 5 && x < 10)
    {
        cout << fixed << setprecision(3) << 2 - 1.5 * (x- 3) * (x - 3) << endl;
    }
    else
    {
        cout << fixed << setprecision(3) << x / 2 - 1.5 << endl;
    }
    return 0;
}