题目描述
求方程 的根,用三个函数分别求当b^2-4ac大于0、等于0、和小于0时的根,并输出结果。从主函数输入a、b、c的值。
输入
a b c
输出
x1=? x2=?
样例输入
4 1 1
样例输出
x1=-0.125+0.484i x2=-0.125-0.484i
提示
来源
#include <stdio.h>#include <math.h>int main(){ float a,b,c,t,x1,x2; scanf("%f%f%f",&a,&b,&c); t=b*b-4*a*c; if(t>0) { x1=(sqrt(t)-b)/(2*a); x2=(b+sqrt(t))/(2*a); printf("x1=%.3f x2=%.3f",x1,x2); } if(t==0) { x1=-b/(2*a); x2=x1; printf("x1=%.3f x2=%.3f",x1,x2); } if(t<0) { printf("x1=%.3f+%.3fi x2=%.3f-%.3fi",-b/(2*a),sqrt(-t)/(2*a),-b/(2*a),sqrt(-t)/(2*a)); } }