📜  找出复数的共轭

📅  最后修改于: 2021-04-23 18:59:24             🧑  作者: Mango

给定字符串形式的复数str ,任务是确定此复数的共轭数。
例子:

Input: str = "3 - 4i"
Output: 3 + 4i

Input: str = "6 - 5i"
Output: 6 + 5i

方法:如果仅是两个数字的虚部的符号不同,则将一个复数称为另一个复数的共轭。

If complex number = x + iy

Conjugate of this complex number = x - iy

下面是上述方法的实现:

C++
// C++ implementation to Find the
// conjugate of a complex number
#include 
using namespace std;
  
// Function to find conjugate
// of a complex number
void solve(string s)
{
    string z = s;
    int l = s.length();
    int i;
  
    if (s.find('+') < l) {
  
        // store index of '+'
        i = s.find('+');
  
        replace(s.begin(),
                s.end(),
                '+', '-');
    }
    else {
  
        // store index of '-'
        i = s.find('-');
  
        replace(s.begin(),
                s.end(),
                '-', '+');
    }
  
    // print the result
    cout << "Conjugate of "
         << z << " = "
         << s << endl;
}
  
// Driver code
int main()
{
  
    // initialise the complex number
    string s = "3-4i";
  
    solve(s);
  
    return 0;
}


Java
// Java implementation to Find the
// conjugate of a complex number
  
class GFG
{
    // Function to find conjugate
    // of a complex number
    static void solve(String s)
    {
        String z = s;
        int l = s.length();
        int i;
        String str;
  
        if (s.indexOf('+') != -1) {
       
            // store index of '+'
            i = s.indexOf('+');
       
            str = s.replace('+', '-');
        }
        else {
       
            // store index of '-'
            i = s.indexOf('-');
       
            str = s.replace('-', '+');
        }
       
        // print the result
        System.out.println("Conjugate of "
             + z + " = "
             + str);
    }
       
    // Driver code
    public static void main(String []args)
    {
       
        // initialise the complex number
        String s = "3-4i";
       
        solve(s);
    }
}
  
// This code is contributed by chitranayal


Python3
# Python3 implementation to Find the 
# conjugate of a complex number 
  
# Function to find conjugate 
# of a complex number 
def solve(s):
    z = s
    l = len(s) 
    i = 0
    if (s.find('+') != -1):
    
        # store index of '+' 
        i = s.find('+')
    
        s = s.replace('+', '-')
    else:
        # store index of '-' 
        i = s.find('-')
  
        s = s.replace('-', '+',1)
    
    # print the result 
    print("Conjugate of ",z," = ",s)
  
  
# Driver code 
  
# initialise the complex number 
s = "3-4i"
solve(s)
  
# This code is contributed by Sanjit_Prasad


C#
// C# implementation to find the
// conjugate of a complex number
using System;
  
class GFG{
      
// Function to find conjugate
// of a complex number
static void solve(String s)
{
    String z = s;
    int l = s.Length;
    int i;
    String str;
  
    if (s.IndexOf('+') != -1) 
    {
          
        // Store index of '+'
        i = s.IndexOf('+');
  
        str = s.Replace('+', '-');
    }
    else 
    {
  
        // Store index of '-'
        i = s.IndexOf('-');
  
        str = s.Replace('-', '+');
    }
  
    // print the result
    Console.WriteLine("Conjugate of "+ z +
                      " = " + str);
}
  
// Driver code
public static void Main(String []args)
{
  
    // Initialise the complex number
    String s = "3-4i";
  
    solve(s);
}
}
  
// This code is contributed by amal kumar choubey


输出:
Conjugate of 3-4i = 3+4i