解题思路:
注意事项:
参考代码:
import cmath
def greater_p(a,b,c):
p = b**2-4*a*c
x1 = (-b + cmath.sqrt(p)) / (2*a)
x2 = (-b - cmath.sqrt(p)) / (2*a)
return x1,x2
def loss_p(a,b,c):
p = b**2-4*a*c
x1 = (-b + cmath.sqrt(p)) / (2*a)
x2 = (-b - cmath.sqrt(p)) / (2*a)
return x1,x2
def equal_p(a,b,c):
x = -b/(4*a)
return x,x
def main():
a,b,c=map(int,input().strip().split())
p = b**2-4*a*c
if p > 0:
x1,x2 = greater_p(a,b,c)
print(f"x1={x1:.3f} x2={x2:.3f}")
elif p == 0:
x1,x2 = equal_p(a,b,c)
print(f"x1={x1:.3f} x2={x2:.3f}")
else:
x1,x2 = loss_p(a,b,c)
if x1.imag < 0 and x2.imag < 0:
print(f"x1={x1.real:.3f}{x1.imag:.3f}i x2={x2.real:.3f}{x2.imag:.3f}i")
elif x1.imag > 0 and x2.imag < 0:
print(f"x1={x1.real:.3f}+{x1.imag:.3f}i x2={x2.real:.3f}{x2.imag:.3f}i")
elif x1.imag > 0 and x2.imag > 0:
print(f"x1={x1.real:.3f}+{x1.imag:.3f}i x2={x2.real:.3f}+{x2.imag:.3f}i")
elif x1.imag < 0 and x2.imag > 0:
print(f"x1={x1.real:.3f}{x1.imag:.3f}i x2={x2.real:.3f}+{x2.imag:.3f}i")
if __name__ == "__main__":
main()
0.0分
0 人评分
【蟠桃记】 (C++代码)(递归计算)浏览:1061 |
点我有惊喜!你懂得!浏览:1166 |
C语言程序设计教程(第三版)课后习题10.7 (C语言代码)浏览:549 |
C语言训练-求s=a+aa+aaa+aaaa+aa...a的值 (C++代码)(手动优化一下计算)浏览:1366 |
C语言程序设计教程(第三版)课后习题5.5 (C语言代码)浏览:577 |
C语言程序设计教程(第三版)课后习题6.11 (C语言代码)浏览:567 |
【偶数求和】 (C语言代码)浏览:588 |
printf基础练习2 (C语言代码)浏览:691 |
a+b浏览:452 |
DNA (C语言代码)浏览:798 |