请输入二次项系数a:1
请输入一次项系数b:6
请输入常数项c:3
该一元二次方程有两个实数根,分别是
x1=-0.55*2=-5.45
from numpy.lib.scimath import sqrt
a = int(input())
b = int(input())
c = int(input())
if a == 0:
if b == 0 and c ==0:
print("There are infinitely many solutions!")
else:
if b==0:
print ("There is no solution")
else:
print("There is a unique solution x=%g" %(-1*c/b))
else:
disc = b*b -4*a*c
if disc > 0:
x1 = (-b + sqrt(disc)) / 2 / a
x2 = (-b - sqrt(disc)) / 2 / a
if disc > 0:
print("There are two distinct real roots x1=%g and x2=%g" % (x1, x2))
else:
print("There are two conjugate roots x1=%g and x2=%g" % (x1, x2))
else:
if disc == 0:
print("There are two equal real roots x1=x2=%g" % (-1 * b / 2 / a))
else:
x1 = (-b + sqrt(disc)) / 2 / a
x2 = (-b - sqrt(disc)) / 2 / a
print("There are two conjugate roots x1=%s and x2=%s" % (x1, x2))
如有帮助,采纳支持一下,谢谢。
import math
def is_real_root(a,b,c):
return (b * b - 4 * a * c) < 0
def get_real_root(a,b,c):
if(a == 0):
print('此方程不是一元二次方程!')
elif(is_real_root(a,b,c)):
print('此方程一元二次方程无实数根!')
else:
x1 = (-b + math.sqrt(b * b - 4 * a * c))/(2 * a)
x2 = (-b - math.sqrt(b * b - 4 * a * c))/(2 * a)
print(f'该一元二次方程有两个实数根,分别是:x1={round(x1,2)},x2={round(x2,2)}')
if __name__ == '__main__':
a = int(input('请输入二次项系数a:'))
b = int(input('请输入一次项系数b:'))
c = int(input('请输入常数项c:'))
get_real_root(a,b,c)