分段函数
题目描述
编写程序,计算下列分段函数 y = f ( x ) y=f(x) y=f(x) 的值。
当 0 ≤ x < 5 0 \le x<5 0≤x<5 时, y = − x + 2.5 y=-x+2.5 y=−x+2.5。
当 5 ≤ x < 10 5 \le x<10 5≤x<10 时, y = 2 − 1.5 ( x − 3 ) ( x − 3 ) y=2-1.5(x-3)(x-3) y=2−1.5(x−3)(x−3)。
当 10 ≤ x < 20 10 \le x<20 10≤x<20 时, y = x / 2 − 1.5 y=x/2-1.5 y=x/2−1.5。
输入格式
一个浮点数 x x x。
输出格式
输出 x x x 对应的分段函数值: f ( x ) f(x) f(x)。结果保留到小数点后三位。
样例 #1
样例输入 #1
1.0
样例输出 #1
1.500
提示说明
对于 100% 的数据 0 ≤ x < 20 0 \le x<20 0≤x<20
代码内容
// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <stack>//栈
// #include <deque>//队列
// #include <queue>//堆/优先队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
double x;
cin>>x;
double y;
if(x<5) y=-x+2.5; //0<=x<5
else if(x<10) y=2-1.5*(x-3)*(x-3); //5<=x<10
else y=x/2-1.5; //10<=x<20
printf("%.3lf",y);
return 0;
}