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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM