原文:https://www . geeksforgeeks . org/复数乘积使用三乘法运算/

给定四个整数 a、b、c 和 d ,它们表示形式为 (a bi)(c di) 的两个复数,任务是仅使用三次乘法运算来找到给定复数的乘积。 示例:

输入: a = 2,b = 3,c = 4,d = 5 输出: -7 22i 说明: 产品给出为: (2 3i)(4 5i)= 2 * 4 4 * 3i 2 * 5i 3 * 5 (1) = 8–15 (12 10)i =-7 22i【t10

天真方法:天真方法是将给定的两个复数直接相乘,如下所示:

= >(a bi)(c di) =>a(c di) b * i(c di) =>a * c ad * i b * c * i b * d * i * i =>(a * c–b * d) (a * d b * c) i

上述运算需要四次乘法才能求出两个复数的乘积。 高效方法:上述方法需要四次乘法才能找到乘积。可以简化为三次乘法如下: 两个复数的乘法如下:

(a bi)*(c di)= a * c–b * d (a * d b * c)i

简化实数部分:

实部= a * c–b * d 让 prod1 = ac,prod 2 = b * d 因此,实部= prod 1–prod 2*

将虚部简化如下:

虚数部分= ad bc 在上面的想象部分中加减 ac 和 bd 我们有, 虚数部分= a * c–a * c a * d b * c b * d–b * d, 关于重新排列我们得到的项, =>a * b b * c a * d b * d–a * c–b * d =>(a b) c (a b) d–a * c–b * d =>(a b)(c d)–a * c–b * d 让 prod3 = (a b)(c d) 然后虚部由prod 3 –( prod 1 prod)给出

因此,我们需要找到 prod1 = a * cprod2 = b * dprod3 = ( a b ) * ( c d ) 的值。 那么,我们的最终答案将是:

实部= prod 1–prod 2 虚部= prod 3 –( prod 1 prod 2)

下面是上述方法的实现:

c

// c   program for the above approach
#include 
using namespace std;
// function to multiply complex
// numbers with just three
// multiplications
void print_product(int a, int b,
                   int c, int d)
{
    // find value of prod1, prod2 and prod3
    int prod1 = a * c;
    int prod2 = b * d;
    int prod3 = (a   b) * (c   d);
    // real part
    int real = prod1 - prod2;
    // imaginary part
    int imag = prod3 - (prod1   prod2);
    // print the result
    cout << real << "   " << imag << "i";
}
// driver code
int main()
{
    int a, b, c, d;
    // given four numbers
    a = 2;
    b = 3;
    c = 4;
    d = 5;
    // function call
    print_product(a, b, c, d);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program for the above approach
class gfg{
// function to multiply complex
// numbers with just three
// multiplications
static void print_product(int a, int b,
                          int c, int d)
{
    // find value of prod1, prod2 and prod3
    int prod1 = a * c;
    int prod2 = b * d;
    int prod3 = (a   b) * (c   d);
    // real part
    int real = prod1 - prod2;
    // imaginary part
    int imag = prod3 - (prod1   prod2);
    // print the result
    system.out.println(real   "   "  
                       imag   "i");
}
// driver code
public static void main(string[] args)
{
    // given four numbers
    int a = 2;
    int b = 3;
    int c = 4;
    int d = 5;
    // function call
    print_product(a, b, c, d);
}
}
// this code is contributed by pratima pandey

python 3

# python3 program for the above approach
# function to multiply complex
# numbers with just three
# multiplications
def print_product(a, b, c, d):
    # find value of prod1, prod2
    # and prod3
    prod1 = a * c
    prod2 = b * d
    prod3 = (a   b) * (c   d)
    # real part
    real = prod1 - prod2
    # imaginary part
    imag = prod3 - (prod1   prod2)
    # print the result
    print(real, "   ", imag, "i")
# driver code
# given four numbers
a = 2
b = 3
c = 4
d = 5
# function call
print_product(a, b, c, d)
# this code is contributed by vishal maurya.

c

// c# program for the above approach
using system;
class gfg{
// function to multiply complex
// numbers with just three
// multiplications
static void print_product(int a, int b,
                          int c, int d)
{
    // find value of prod1, prod2 and prod3
    int prod1 = a * c;
    int prod2 = b * d;
    int prod3 = (a   b) * (c   d);
    // real part
    int real = prod1 - prod2;
    // imaginary part
    int imag = prod3 - (prod1   prod2);
    // print the result
    console.write(real   "   "   imag   "i");
}
// driver code
public static void main()
{
    int a, b, c, d;
    // given four numbers
    a = 2;
    b = 3;
    c = 4;
    d = 5;
    // function call
    print_product(a, b, c, d);
}
}
// this code is contributed by code_mech

java 描述语言


output: 

-7   22i

时间复杂度:o(1) t5】辅助空间: o(1)