编写程序,输入 a 、 b 、 c ,求一元二次方程 ax + bx + c =0的根。提示:①当 a =0时, x =-c/b 。 ②当 a ≠0时, det =b^2-4ac: ·det=0,两个相等实根: · det >0,两个相异实根: · det <0,两个相异复数根。
时间: 2024-06-01 16:10:45 浏览: 31
a = float(input("请输入a的值:"))
b = float(input("请输入b的值:"))
c = float(input("请输入c的值:"))
if a == 0:
x = -c / b
print("该方程为一元一次方程,解为:", x)
else:
det = b ** 2 - 4 * a * c
if det > 0:
x1 = (-b + det ** 0.5) / (2 * a)
x2 = (-b - det ** 0.5) / (2 * a)
print("该方程有两个不相等的实根,分别为:", x1, x2)
elif det == 0:
x = -b / (2 * a)
print("该方程有两个相等的实根,解为:", x)
else:
real_part = -b / (2 * a)
imag_part = (-det) ** 0.5 / (2 * a)
print("该方程有两个共轭的复数根,分别为:", real_part, "+", imag_part, "i", "和", real_part, "-", imag_part, "i")
相关问题
编写程序,输入 a 、 b 、 c ,求一元二次方程 ax + bx + c =0的根
以下是Python代码实现:
```python
import math
a = float(input("请输入a:"))
b = float(input("请输入b:"))
c = float(input("请输入c:"))
delta = b ** 2 - 4 * a * c
if delta < 0:
print("该方程无实数根")
elif delta == 0:
x = -b / (2 * a)
print("该方程有唯一实数根x =", x)
else:
x1 = (-b + math.sqrt(delta)) / (2 * a)
x2 = (-b - math.sqrt(delta)) / (2 * a)
print("该方程有两个实数根x1 =", x1, ",x2 =", x2)
```
首先,我们输入三个系数a、b、c。然后,我们计算判别式delta,如果delta小于0,则方程无实数根;如果delta等于0,则方程有唯一实数根,其值为-x / 2a;如果delta大于0,则方程有两个实数根,其值为(-b ± sqrt(delta)) / 2a。最后,我们输出根的值。
编写c语言程序,求一元二次方程ax²+bx+c=0的根
```c
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, delta, x1, x2;
printf("请输入一元二次方程的三个系数:\n");
scanf("%lf%lf%lf", &a, &b, &c);
delta = b * b - 4 * a * c;
if (delta > 0) {
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
printf("方程有两个不相等的实根:x1=%.2f, x2=%.2f\n", x1, x2);
} else if (delta == 0) {
x1 = x2 = -b / (2 * a);
printf("方程有两个相等的实根:x1=x2=%.2f\n", x1);
} else {
printf("方程没有实根\n");
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)