Python 求一元二次方程的根(包括虚根)

Python 求一元二次方程的根(包括虚根)_第1张图片

import math
a = float(input())
b = float(input())
c = float(input())
delta = b * b - 4 * a * c
if(delta>0):
    x1 = (-1 * b + math.sqrt(delta)) / (2 * a)
    x2 = (-1 * b - math.sqrt(delta)) / (2 * a)
    print(x1)
    print(x2)
elif(delta == 0):
    x = (-1 * b ) / (2 * a)
    print(x)
elif(delta < 0): #a b c: 1 2 8
    shi = -1 * b/ (2 * a)
    xu = math.sqrt(-1 * delta) / (2 * a)
    print(f'x1={shi}+{xu}i') #x1=-1.0+2.6457513110645907i
    print(f'x2={shi}-{xu}i') #x2=-1.0-2.6457513110645907i

你可能感兴趣的:(Python,python,算法,开发语言)