Discuss / Python / 作业

作业

Topic source

Nuomi1

#1 Created at ... [Delete] [Delete and Lock User]
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import math

def quadratic(a, b, c):
    Δ = b*b - 4*a*c
    if Δ > 0:
        x1 = (-b+math.sqrt(Δ)) / (2*a)
        x2 = (-b-math.sqrt(Δ)) / (2*a)
        return x1, x2
    elif Δ == 0:
        x1 = -b / (2*a)
        return x1
    else:
        Δ = -Δ
        real = -b / (2*a)
        imaginary = math.sqrt(Δ) / (2*a)
        x1 = '%.2f' % real + '+' + '%.2f' % imaginary + 'i'
        x2 = '%.2f' % real + '-' + '%.2f' % imaginary + 'i'
        return x1, x2

  • 1

Reply