📜  将N个复数相乘为字符串

📅  最后修改于: 2021-04-23 06:54:00             🧑  作者: Mango

给定N个以字符串形式出现的复数,任务是打印这N个复数的乘法。

例子:

方法

  • 首先,从头开始进行迭代,并使用前两个String并将其擦除。
  • 接下来,将字符串转换为带有适当符号的数字。将字符串的实部和虚部存储在单独的变量中。以作为第一字符串的实部,而B为第一个字符串的虚部。取C作为第二字符串的实部,而d为第二字符串的虚部。
  • 接下来,我们将通过计算ac的乘积,然后将其与b的乘积相减,再将bd的乘积相减,来计算实数的结果值。对于虚部,我们将计算ad的乘积之和以及bc的乘积。
  • 然后,我们将生成一个临时字符串,以获取较早计算出的实和虚值的总和。
  • 我们将结果字符串推入向量。重复上述步骤,直到向量中仅剩一个元素。
  • 返回向量中剩余的最后一个元素,这是所需的答案。

以下是我们上述方法的实现:

C++
// C++ Program to multiply
// N complex Numbers
#include 
using namespace std;
 
#define ll long long
 
// Function which returns the
// string in digit format
vector findnum(string s1)
{
    vector v;
    // a : real
    // b : imaginary
    int a = 0, b = 0;
    int sa = 0, sb = 0, i = 0;
    // sa : sign of a
    // sb : sign of b
    if (s1[0] == '-') {
        sa = 1;
        i = 1;
    }
    // Extract the real number
    while (isdigit(s1[i])) {
        a = a * 10 + (int(s1[i]) - 48);
        i++;
    }
 
    if (s1[i] == '+') {
        sb = 0;
        i += 1;
    }
 
    if (s1[i] == '-') {
        sb = 1;
        i += 1;
    }
 
    // Extract the imaginary part
    while (i < s1.length() && isdigit(s1[i])) {
        b = b * 10 + (int(s1[i]) - 48);
        i++;
    }
 
    if (sa)
        a *= -1;
 
    if (sb)
        b *= -1;
 
    v.push_back(a);
    v.push_back(b);
 
    return v;
}
 
string complexNumberMultiply(vector v)
{
    // if size==1 means we reached at result
    while (v.size() != 1) {
        // Extract the first two elements
        vector v1 = findnum(v[0]);
        vector v2 = findnum(v[1]);
 
        // Remove them
        v.erase(v.begin());
        v.erase(v.begin());
 
        // Calculate and store the real part
        ll r = (v1[0] * v2[0] - v1[1] * v2[1]);
        // Calculate and store the imaginary part
        ll img = v1[0] * v2[1] + v1[1] * v2[0];
 
        string res = "";
        // Append the real part
        res += to_string(r);
        res += '+';
        // Append the imaginary part
        res += to_string(img) + 'i';
 
        // Insert into vector
        v.insert(v.begin(), res);
    }
 
    return v[0];
}
 
// Driver Function
int main()
{
    int n = 3;
    vector v = { "3+1i",
                         "2+1i", "-5+-7i" };
 
    cout << complexNumberMultiply(v) << "\n";
 
    return 0;
}


Python3
# Python3 program to multiply
# N complex Numbers
 
# Function which returns the
# in digit format
def findnum(s1):
     
    v = []
     
    # a : real
    # b : imaginary
    a = 0
    b = 0
    sa = 0
    sb = 0
    i = 0
     
    # sa : sign of a
    # sb : sign of b
    if (s1[0] == '-'):
        sa = 1
        i = 1
 
    # Extract the real number
    while (s1[i].isdigit()):
        a = a * 10 + (int(s1[i]))
        i += 1
 
    if (s1[i] == '+'):
        sb = 0
        i += 1
 
    if (s1[i] == '-'):
        sb = 1
        i += 1
 
    # Extract the imaginary part
    while (i < len(s1) and s1[i].isdigit()):
        b = b * 10 + (int(s1[i]))
        i += 1
 
    if (sa):
        a *= -1
 
    if (sb):
        b *= -1
 
    v.append(a)
    v.append(b)
 
    return v
 
def complexNumberMultiply(v):
     
    # If size==1 means we reached at result
    while (len(v) != 1):
         
        # Extract the first two elements
        v1 = findnum(v[0])
        v2 = findnum(v[1])
 
        # Remove them
        del v[0]
        del v[0]
 
        # Calculate and store the real part
        r = (v1[0] * v2[0] - v1[1] * v2[1])
         
        # Calculate and store the imaginary part
        img = v1[0] * v2[1] + v1[1] * v2[0]
 
        res = ""
         
        # Append the real part
        res += str(r)
        res += '+'
         
        # Append the imaginary part
        res += str(img) + 'i'
 
        # Insert into vector
        v.insert(0, res)
 
    return v[0]
 
# Driver code
if __name__ == '__main__':
     
    n = 3
    v = [ "3+1i", "2+1i", "-5+-7i" ]
 
    print(complexNumberMultiply(v))
 
# This code is contributed by mohit kumar 29


输出:
10+-60i


时间复杂度: O(N)
辅助空间: O(N)