习题3.1 (2)

#include<stdio.h>
main()
{
    int x = 32,y = 81,p,q;
    p = x++;
    q = --y;
    printf("%d%d\n",p,q);
    printf("%d%d\n",x,y);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我理解了您的问题。这是一个求解二元方程的问题,如果行列式小于0,则使用复数类来得到虚根。 以下是我使用 Python 代码进行实现,您可以参考: ```python import math class Complex: def __init__(self, real=0, imag=0): self.real = real self.imag = imag def __add__(self, other): return Complex(self.real + other.real, self.imag + other.imag) def __sub__(self, other): return Complex(self.real - other.real, self.imag - other.imag) def __mul__(self, other): return Complex(self.real * other.real - self.imag * other.imag, self.real * other.imag + self.imag * other.real) def __truediv__(self, other): r = (other.real ** 2 + other.imag ** 2) return Complex((self.real * other.real + self.imag * other.imag) / r, (self.imag * other.real - self.real * other.imag) / r) def __abs__(self): return math.sqrt(self.real ** 2 + self.imag ** 2) def __str__(self): if self.imag >= 0: return "{:.1f}+{:.1f}i".format(self.real, self.imag) else: return "{:.1f}{:.1f}i".format(self.real, self.imag) def main(): a, b, c = map(float, input("Enter a, b, c: ").split()) d = b ** 2 - 4 * a * c if d >= 0: r1 = (-b + math.sqrt(d)) / (2 * a) r2 = (-b - math.sqrt(d)) / (2 * a) print("The roots are {:.5f} and {:.5f}".format(r1, r2)) else: r1 = Complex(-b / (2 * a), math.sqrt(-d) / (2 * a)) r2 = Complex(-b / (2 * a), -math.sqrt(-d) / (2 * a)) print("The roots are {} and {}".format(r1, r2)) if __name__ == '__main__': main() ``` 在这个实现,我们定义了一个名为 Complex 的类来表示复数,并实现了一些基本的操作。在主函数,我们首先读入 a、b、c 三个系数,然后计算行列式 d。如果 d 大于等于 0,则使用一般的求根公式得到实根,否则使用 Complex 类得到虚根。 以下是一些示例输入输出: ``` Enter a, b, c: 13 1 1 The roots are -0.3820 and -2.6180 ``` ``` Enter a, b, c: 1 2 1 The root is -1.0000 ``` ``` Enter a, b, c: 1 2 3 The roots are -1.0000+1.4142i and -1.0000-1.4142i ``` 希望这个示例代码对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值