为了账号安全,请及时绑定邮箱和手机立即绑定

本人学生,oj平台一道题一直通不过敢问哪里出错。

本人学生,oj平台一道题一直通不过敢问哪里出错。

C
慕虎8333395 2017-10-27 23:44:53
#include<stdio.h>#include<math.h>int main(){ float a,b,c,d,f1,f2,y,p,m; scanf("%f %f %f",&a,&b,&c); d=b*b-4*a*c; y=sqrt(d); f1=(-b+y)/(2*a); f2=(-b-y)/(2*a); if(a==0) { printf("The equation is not quadratic."); } else if(a!=0) { if(d==0) { printf("The eaquation has two equal roots: %.4f.",f1);  } else if(d>0) { y=sqrt(d); printf("The eaquation has two distinct real roots: %.4f and %.4f.",f1,f2); } else if(d<0) { m=sqrt(-d)/(2*a); p=-b/(2*a); printf("The eaquation has two complex roots: %.4f+%.4fi and %.4f-%.4fi.",p,m,p,m); } } return 0; } 
查看完整描述

2 回答

?
慕沐8454250

TA贡献1条经验 获得超0个赞

#include<stdio.h>

#include<math.h>

int main()

{

float a, b, c, d, f1, f2, y;

scanf_s("%f %f %f", &a,&b,&c);

d = b*b - 4 * a*c;

y = (float)sqrt(d);

if (a == 0)

{

printf("The equation is not quadratic.");

}

else if (a != 0)

{

f1 = (-b + y) / (2 * a);

f2 = (-b - y) / (2 * a);

if (d == 0)

{

printf("The eaquation has two equal roots: %.4f.", f1);

}

else if (d>0)

{

printf("The eaquation has two distinct real roots: %.4f and %.4f.", f1, f2);

}

else if (d<0)

{

printf("The eaquation has two complex roots: %.4f and %.4f",f1,f2);

}

}

system("pause");

return 0;

}


查看完整回答
反对 回复 2017-10-28
?
慕虎8333395

TA贡献2条经验 获得超0个赞

求ax2+bx+c=0的根。分别考虑d=b2-4ac大于零,等于零和小于零三种情况。

a、b、c要求是浮点型。程序要对a、b、c的各种情况进行处理。如判断a是否为0,b2-4ac分别为大于0、小于0、等于0。

解答提示:

1)求一浮点数的平方根可以用库函数sqrt(x)。x要求是浮点数。如以下赋值语句:y=sqrt(x);表示求x的平方根,赋值给y。为了使用该函数,需要在main函数之前加上预处理语句:#include<math.h>。

2)如何判断两个浮点数是否相等:

假设f1和f2是两个浮点数。若想写一个关系表达式,判断f1和f2是否相等,不能写成:if(f1==f2),而是要写成f1和f2的差的绝对值近似接近于0,如写成:if(fabs(f1-f2)<=1e-6)。其中1e-6表示10的-6次方,fabs函数用于求绝对值。原因:浮点数在内存中是以近似值存储的,所以不能执行是否相等的比较。

输入格式


输入3个浮点数,代表a,b,c。

输出格式


输出对应方程的根:

当该方程非一元二次方程时,输出“The equation is not quadratic.”。

当该一元二次方程有两个相等的实数根时,输出“The equation has two equal roots: x.”。

当该一元二次方程有两个不相等的实数根时,输出“The equation has two distinct real roots: x1 and x2.”。(x1  > x2)

当该一元二次方程有两个不相等的虚数根时,输出“The equation has two complex roots: x1 and x2.”。

 

若x为虚数,则x1的格式为a+bi,x2的格式为a-bi.(其中a,b保留4位有效数字, 例如:3.0000+3.0000i)

所有的数均保留4位有效数字。


查看完整回答
反对 回复 2017-10-28
?
慕虎8333395

TA贡献2条经验 获得超0个赞

//img1.sycdn.imooc.com//59f3548a0001b94809410611.jpg这是题目

查看完整回答
反对 回复 2017-10-27
  • 2 回答
  • 1 关注
  • 2229 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信