编写程序计算y=f(x)函数的值,其中y=x,当x<1时,y=-1/×-1,1<=×<10时,y=5*x-11,x>=10
float fun(float x)
{
if (x < 1)
return x;
else if (x >= 1 && x < 10)
return -1 / x - 1;
return 5 * x - 11;
}
int main()
{
float x;
scanf_s("%f", &x, 1);
float y = fun(x);
printf("%.2f\n", y);
}
你意思是不是x<1的时候,y=x;
double Function(double x)
{
if(x < 1)
{
return x;
}
else if(x >= 1 && x < 10)
{
return -1/x - 1;
}
else
{
return 5*x - 11;
}
}
代码如下,如有帮助,请采纳一下,谢谢:
float func(float x)
{
if (x < 1)
{
return x;
}else if (x >=1 && x < 10)
{
return -1.0/x -1;
}else
return 5.0*x -11;
}
#include<cstdio>
#include<cmath>
const double esp = 1e-6;
float func(float x)
{
if (1-x>esp)
{
return x;
}
else if (x-1>=esp && 10-x>=esp)
{
return -1.0/x -1;
}else
return 5.0*x -11;
}