题解 | #计算一元二次方程#
计算一元二次方程
http://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
- 解题注意点:负数用实部和虚部合成,不要用complex,这个虚部的符号是j,不是i。。。。。
import math
lines=[]
while True:
try:
lines.append(input())
except:
break
for i in lines:
a,b,c = list(map(float,i.split(' ')))
if a == 0:
print("Not quadratic equation")
else:
temp = b**2 - 4*a*c
if temp == 0:
x = -b/(2*a)
print(f"x1=x2={x:.2f}")
elif temp>0:
x1 = -b/(2*a) - math.sqrt(temp) /(2*a)
x2 = -b/(2*a) + math.sqrt(temp) /(2*a)
print(f"x1={x1:.2f};x2={x2:.2f}")
else:
x = -b/(2*a)
y = math.sqrt(-temp) /(2*a)
print(f"x1={x:.2f}-{y:.2f}i;x2={x:.2f}+{y:.2f}i")