算法1
- 当 0≤x<2 时,y=−x+2.5;
if(x>=0&&x<2)y=-x+2.5;
- 当 2≤x<4 时,y=2−1.5(x−3)(x−3);
else if(x>=2&&x<4)y=2-1.5*(x-3)*(x-3);
- 当 4≤x<6,y=x/2−1.5。
else y=x/2-1.5;
参考文献
C++ 代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
double x;
cin>>n;
while(n--){
double y=0;
cin>>x;
if(x>=0&&x<2)y=-x+2.5;
else if(x>=2&&x<4)y=2-1.5*(x-3)*(x-3);
else y=x/2-1.5;
printf("y=%.1lf\n",y);
}
}