给定四个表示形式(a + bi)和(c + di)的两个复数的整数a,b,c和d ,任务是仅使用三个乘法运算来找到给定复数的乘积。
例子:
Input: a = 2, b = 3, c = 4 and d = 5
Output: -7 + 22i
Explanation:
Product is given by:
(2 + 3i)*(4 + 5i) = 2*4 + 4*3i + 2*5i + 3*5*(-1)
= 8 – 15 + (12 + 10)i
= -7 + 22i
Input: a = 3, b = 7, c = 6 and d = 2
Output: 4 + 48i
天真的方法:天真的方法是将给定的两个复数直接相乘为:
=> (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
简化实部:
real part = a*c – b*d
Let prod1 = a*c and prod2 = b*d.
Thus, real part = prod1 – prod2
简化虚构部分,如下所示:
imaginary part = a*d + b*c
Adding and subtracting a*c and b*d in the above imaginar part we have,
imaginary part = a*c – a*c + a*d + b*c + b*d – b*d,
On rearranging the terms we get,
=> 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
Let prod3 = (a + b)*(c + d)
Then the imaginary part is given by prod3 – (prod1 + prod2).
因此,我们需要找到prod1 = a * c , prod2 = b * d和prod3 =(a + b)*(c + d)的值。
因此,我们的最终答案将是:
Real Part = prod1 – prod2
Imaginary Part = prod3 – (prod1 + prod2)
下面是上述方法的实现:
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
Python3
# 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
-7 + 22i
时间复杂度: O(1)
辅助空间: O(1)