利用三次实数相乘实现两复数相乘代码
两复数相乘计算:
(a + bi) * (c + di) = (a*c - b*d) + (a*d + b*c)i
需要四次乘法,两次加法,而可以通过下面的方法优化:
A = (a + b) * c
B = (c + d) * b
C = (b - a) * d
(a + bi) * (c + di) = (A - B) + (B - C)i
这样需要三次乘法五次加法,而对于计算机而言计算加法显然比乘法快很多,可以用此方法来优化复数计算。
#include<stdio.h>
#include<math.h>
int play(double a, double b, double c, double d)
{
double A,B,C,m,n;
A = (a + b) * c;
B = (c + d) * b;
C = (b - a) * d;
m = (A - B);
n = (B - C);
printf("%.2lf", m);
printf("+%.2lfi", n);
return 0;
}
int main()
{
double a1, a2, b1, b2;
double m, n, c = 0;
scanf_s("%lf%lf%lf%lf", &a1, &b1, &a2, &b2);
play(a1,b1,a2,b2);
}