002: seeking one yuan quadratic equation roots

Total time limit: 

1000ms
 
Memory Limit: 
65536kB
description

Using the formula x1 = (-b + sqrt (b * b-4 * a * c)) / (2 * a), x2 = (-b - sqrt (b * b-4 * a * c)) / (2 * a) find a quadratic equation AX 2 + BX + C = 0 of the root, wherein a is not equal to zero.

Entry
Input line, comprising three floating point numbers a, b, c (separated by a space between them), respectively equation AX 2  + BX + C = 0 of coefficients.
Export
Output a line indicating solution of the equation.
If the B 2  = A * C *. 4, the two real roots are equal, the output form: x1 = x2 = ....
If the B 2  > A * C *. 4, two real roots is not equal, the output form: x1 = ...; x2 = ... , where x1> x2.
If the B 2  <C * A *. 4, there are two imaginary roots, output: x1 = the real portion of the imaginary part + i; x2 = the real part - imaginary part i, i.e., the imaginary part of the coefficient is equal to the imaginary part is greater than x1 and x2 coefficient, the real part is 0 can not be omitted. Real = -b / (2 * a) , the imaginary part = sqrt (4 * a * cb * b) / (2 * a)

All real part requires accurate to five decimal places, there is no space between the numbers, symbols.
Sample input
Input Sample 1 
1.0 2.0 8.0 

Sample Input 2 
1 1 0
Sample Output
Sample output. 1 
X1 = -1.00000 + 2.64575i; X2 = -1.00000-2.64575i 

sample output 2 
X1 = 0.00000 + 1.00000i; X2 = 0.00000-1.00000i
 1 #include<iostream>
 2 #include <math.h>
 3 #include <cmath>
 4 using namespace std;
 5 
 6 int main(){    
 7     double a=1.0,b=2.0,c=8.0;
 8     double x1=0.0,x2=0.0;
 9     double eps=0.000001;
10     //cin>>a>>b>>c;
11     scanf("%lf%lf%lf",&a,&b,&c);
12     double tmp = b*b - 4*a*c;
13     if(tmp<eps && tmp > -eps){
14         printf("x1=x2=%.5f",(-b)/(2*a)+eps);
15     }else if(tmp >eps){
16         x1=(-b+sqrt(tmp))/(2*a);
17         x2=(-b-sqrt(tmp))/(2*a)+eps;
18         if((x1-x2)>eps){
19             printf("x1=%.5f;x2=%.5f",x1+eps,x2+eps);
20         }else{
21             printf("x1=%.5f;x2=%.5f",x2+eps,x1+eps);
22         }
23     }else{
24         printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi",
25         (-b)/(2*a)+eps,sqrt(-tmp)/(2*a)+eps,(-b)/(2*a)+eps,sqrt(-tmp)/(2*a)+eps);
26 
27     }
28 
29 
30 
31 
32 return 0;
33 } 

 

Guess you like

Origin www.cnblogs.com/geyang/p/12329708.html