09.分段函数

描述

编写程序,计算下列分段函数y=f(x)的值。

y=-x+2.5; 0 <= x < 5

y=2-1.5(x-3)(x-3); 5 <= x < 10

y=x/2-1.5; 10 <= x < 20

输入

一个浮点数N,0 <= N < 20

输出

输出N对应的分段函数值:f(N)。结果保留到小数点后三位。

样例输入

1.0

样例输出

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

int main()
{   
    double x,y;
    cin>>x;
    if(x>=0&&x<5)
        y=-x+2.5;
    else if(x>=5&&x<10)
        y=2-1.5*(x-3)*(x-3);
    else if(x>=10&&x<20)
        y=x/2-1.5;
        cout<<fixed<<setprecision(3)<<y<<endl;
    return 0;
}
发布了30 篇原创文章 · 获赞 3 · 访问量 372

猜你喜欢

转载自blog.csdn.net/qq_45748133/article/details/104338639