求一元二次方程的根pta
时间: 2024-06-13 18:07:43 浏览: 83
以下是求解一元二次方程的根的Python代码:
```python
import math
a, b, c = map(float, input().split())
delta = b * b - 4 * a * c
if delta > 0:
x1 = (-b + math.sqrt(delta)) / (2 * a)
x2 = (-b - math.sqrt(delta)) / (2 * a)
if x1.imag > x2.imag:
x1, x2 = x2, x1
print("{:.2f}+{:.2f}i".format(x1.real, x1.imag))
print("{:.2f}{:.2f}i".format(x2.real, x2.imag))
elif delta == 0:
x = -b / (2 * a)
if x.imag >= 0:
print("{:.2f}+{:.2f}i".format(x.real, x.imag))
else:
print("{:.2f}{:.2f}i".format(x.real, x.imag))
else:
x1 = (-b + math.sqrt(-delta) * 1j) / (2 * a)
x2 = (-b - math.sqrt(-delta) * 1j) / (2 * a)
if x1.imag > x2.imag:
x1, x2 = x2, x1
print("{:.2f}+{:.2f}i".format(x1.real, x1.imag))
print("{:.2f}{:.2f}i".format(x2.real, x2.imag))
```
阅读全文