y=x²+2.5 x<—5
=(x+1)³ -4 —5≤x<2
=(3+4x)½ x≥2
编程程序输入x的值,求y的值
要求:必须用if语句实现
#include<stdio.h>
#include<math.h>
int main()
{
float x;
scanf("%f",&x);
if(x<-5)
{
printf("y = %.2f",pow(x,2)+2.5);
}
else if(x>=-5&&x<2)
{
printf("y = %.2f",pow((x+1),3)-4);
}
else if(x>=2)
{
printf("y = %.2f",sqrt(3+4*x));
}
return 0;
}
解答如下
#include <stdio.h>
#include <math.h>
int main()
{
double x,y;
scanf("%lf",&x);
if(x<-5)
{
y=x*x+2.5*x;
}
else if(-5<x&&x<2)
{
y=pow((x+1),3)-4;
}
else if(x>=2)
{
y=(3+4*x)/2;
}
printf("%lf",y);
return 0;
}
#include<stdio.h>
#include<math.h>
int main()
{
double x,y;
scanf("%f",&x);
if(x<-5)
{
y=xx+2.5;
}
else if(x>=(-5) && x<2 )
{
y=(x+1)*(x+1)*(x+1)-4;
}
else if(x>=2)
{
y=sqrt(4x+3);
}
printf("%f",y);
}
#include <stdio.h>
#include <math.h>
int main(){
double x;
scanf("%lf",&x);
if(x<-5){
printf("%lf\n", x*x+2.5);
} else if(x<2) {
printf("%lf\n",pow(x+1, 3)-4);
} else {
printf("%lf\n",sqrt(3+4*x));
}
return 0;
}
#include<stdio.h>
#include<math.h>
int main()
{
double x,y;
scanf("%f",&x); //输入数字
if(x<-5) //判断不同的情况,使用不同的公式
{
y=x+2.5;
}
else if(x>=(-5) && x<2 )
{
y=(x+1)*(x+1)*(x+1)-4;
}
else if(x>=2)
{
y=sqrt(4x+3); //
}
printf("%f",y); //输出结果
}
代码如下,带显示说明 仅供参考谢谢!
/*
y=x²+2.5 x<—5
y=(x+1)³-4 -5≤x<2
y=(3+4x)½ x≥2
*/
#include<stdio.h>
#include<math.h>
#define Y1(x) ((x)*(x)+2.5) //x < -5
#define Y2(x) (pow((x+1),3)-4) //—5≤ x<2
#define Y3(x) (sqrt(3+4*(x))) //x ≥ 2
int main(int argc, char** argv)
{
double x,y;
printf("\n输入x:");
scanf("%lf",&x);
if(x<-5)
{
y=Y1(x);
printf("\ny=x²+2.5\n=(%lf)²+2.5\n=%lf x<—5时\n",x,y);
}
else if(x>=-5 && x<2)
{
y=Y2(x);
printf("\n y=(x+1)³-4\n=(%lf+1)³-4\n=%lf —5≤x<2时\n" ,x,y);
}
else//因为x已经包含所有情况的唯一情况,所以此处无需加条件了
{
y=Y3(x);
printf("\n y=(3+4x)½\n=(3+4x%lf)½\n=%lf x≥2时\n",x,y);
}
printf("\ny的值为:%lf",y);
return 0;
}
这个问题我感觉不是来写代码的,是来理解(看懂)题主公式的😂