6.(编程)求ax2+bx+c=0方程的解

思路:

1.a=0,方程不是二次方程(是一元一次方程);

2.b2-4ac=0,有两个相等的实根;

3. b2-4ac>0,有两个不等的实更;

4. b2-4ac<0,有两个共轭复根(高中所学为无解,但现在应该以p+qi和p-qi的形式输         出复根,其中p=-b/2a,q=sqrt(b2-4ac)/2a

Ps 共轭复根的两个实数解为:

X1 = -b/2a+i*sqrt(b2-4ac)/2a

X2 = -b/2a-i*sqrt(b2-4ac)/2a

 

 

 1 #include<stdio.h>
 2 #include<math.h>
 3 double a,b,c,p,q;
 4 int main()
 5 {
 6     scanf("%lf %lf %lf",&a,&b,&c);
 7     if((b*b-4*a*c)>0)
 8     {
 9         fun3(a,b,c);
10     }
11     else if((b*b-4*a*c)==0)
12     {
13         fun2(a,b,c);
14     }
15     else
16     {
17         fun1(a,b,c);
18     }
19     return 0;
20 }
21 
22 int fun1(double a,double b,double c)                    //小于0
23 {
24     double m;
25     m=-(b*b-4*a*c);
26     p=-b/(2*a);
27     q=sqrt(m)/(2*a);
28     printf("x1=%.3lf+%.3lfi x2=%.3lf-%.3lfi\n",p,q,p,q);
29     return 0;
30 }
31 
32 int fun2(double a,double b,double c)                    //等于0
33 {
34     p=-b/(2*a);
35     printf("x1=%.3lf x2=%.3lf\n",p,p);
36     return 0;
37 }
38 
39 int fun3(double a,double b,double c)                    //大于0
40 {    
41     p=-b/(2*a);
42     q=(sqrt(b*b-4*a*c))/(2*a);
43     printf("x1=%.3lf x2=%.3lf\n",p+q,p-q);
44     return 0;
45 }

猜你喜欢

转载自www.cnblogs.com/rookieclimber/p/10830913.html