3-4 3.4编程计算一元二次方程的根
3.4. A quadratic equation is an equation that either has the from or an equation that can be algebraically manipulated into this form. In this equation,x is the unknown variable ,and a,b,and c are known constants.Although the constants b and c can be any numbers,including 0, the value of the constant a cannot be 0(if a is 0,the equation would become a liner equation in x).Examples quadratic equations are
三个方程.png
In the first equation a=5,b=6,c=2;in the second equation a=1,b=-7,and c=20;and in the third equation a=34,b=0,and c=16.
The real roots of a quadratic equation can be calculated using the quadratic formula as follows:
第一个根.png
and:
第二个根.png
Using these equations,write a C program to solve for the roots of a quadratic equation.Hint:Tip: no solution shows no real number solution,Accurately six digits after the decimal point(20分)
输入格式:
输入在一行中输入一元二次方程的三个系数a、b和c。
输出格式:
对每一组输入,在一行中输出对应的两个根。
输入样例:
在这里给出一组输入。例如:
1 2 1
输出样例:
在这里给出相应的输出。例如:
-1.000000 -1.000000
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
double rootOne,rootTwo,d;
scanf("%d %d %d",&a,&b,&c);
d=b*b-4*a*c;
if(d<0||a==0)
printf("no real number solution");
if(d>=0&&a!=0)
{
rootOne=((-b+sqrt(d))/(2*a));
rootTwo=((-b-sqrt(d))/(2*a));
printf("%.6lf %.6lf",rootOne,rootTwo);
}
return 0;
}