题解 | 计算一元二次方程
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
from math import sqrt
def compute(a, b, c):
if a == 0:
return "Not quadratic equation"
else:
delta = b ** 2 - 4 * a * c
if delta == 0:
x = -b / (2 * a)
if x == -0.00:
x = 0.00
return f"x1=x2={x:.2f}"
elif delta > 0:
x1 = (-b - sqrt(delta)) / (2 * a)
x2 = (-b + sqrt(delta)) / (2 * a)
return f"x1={x1:.2f};x2={x2:.2f}"
else:
true_part = -b / (2 * a)
artificial_part = sqrt(-delta) / (2 * a)
x1 = f"{true_part:.2f}-{artificial_part:.2f}i"
x2 = f"{true_part:.2f}+{artificial_part:.2f}i"
return f"x1={x1};x2={x2}"
while True:
try:
a, b, c = map(float, input().split())
print(compute(a, b, c))
except:
break
查看15道真题和解析
