Cで2つの複素数を追加するプログラム
a1+ib1とa2+ib2の形式の2つの複素数が与えられた場合、タスクはこれら2つの複素数を加算することです。
複素数は、「a + ib」の形式で表現できる数です。ここで、「a」と「b」は実数であり、iは式の解である虚数です𝑥2=-1 as no実数は方程式を満たしているため、虚数と呼ばれています。
入力
a1 = 3, b1 = 8 a2 = 5, b2 = 2
出力
Complex number 1: 3 + i8 Complex number 2: 5 + i2 Sum of the complex numbers: 8 + i10
説明
(3+i8) + (5+i2) = (3+5) + i(8+2) = 8 + i10
入力
a1 = 5, b1 = 3 a2 = 2, b2 = 2
出力
Complex number 1: 5 + i3 Complex number 2: 2 + i2 Sum of the complex numbers: 7 + i5
説明
(5+i3) + (2+i2) = (5+2) + i(3+2) = 7 + i5
問題を解決するために以下で使用するアプローチは次のとおりです
-
実数と虚数を格納するための構造体を宣言します。
-
入力を受け取り、すべての複素数の実数と虚数を追加します。
アルゴリズム
Start
Decalre a struct complexnum with following elements
1. real
2. img
In function complexnum sumcomplex(complexnum a, complexnum b)
Step 1→ Declare a signature struct complexnum c
Step 2→ Set c.real as a.real + b.real
Step 3→ Set c.img as a.img + b.img
Step 4→ Return c
In function int main()
Step 1→ Declare and initialize complexnum a = {1, 2} and b = {4, 5}
Step 2→ Declare and set complexnum c as sumcomplex(a, b)
Step 3→ Print the first complex number
Step 4→ Print the second complex number
Step 5→ Print the sum of both in c.real, c.img
Stop 例
#include <stdio.h>
//structure for storing the real and imaginery
//values of complex number
struct complexnum{
int real, img;
};
complexnum sumcomplex(complexnum a, complexnum b){
struct complexnum c;
//Adding up two complex numbers
c.real = a.real + b.real;
c.img = a.img + b.img;
return c;
}
int main(){
struct complexnum a = {1, 2};
struct complexnum b = {4, 5};
struct complexnum c = sumcomplex(a, b);
printf("Complex number 1: %d + i%d\n", a.real, a.img);
printf("Complex number 2: %d + i%d\n", b.real, b.img);
printf("Sum of the complex numbers: %d + i%d\n", c.real, c.img);
return 0;
} 出力
上記のコードを実行すると、次の出力が生成されます-
Complex number 1: 1 + i2 Complex number 2: 4 + i5 Sum of the complex numbers: 5 + i7
-
2つの数値を追加するPythonプログラム
この記事では、特定の問題ステートメントを解決するための解決策とアプローチについて学習します。 問題の説明 2つの大きな数が与えられ、それらを追加して出力を表示する必要があります。 ブルートフォースアプローチでは、オペランド間に「+」演算子を使用するか、2つの数値を反復可能に格納して、Python標準ライブラリで使用可能な組み込みのsum関数を使用できます。 このアプローチでは、計算が10進数で直接行われるため、時間計算量が増加します。 次に、10進数のビットを処理する別のアプローチについて説明します。 ここでは、合計とキャリーを計算する加算器の概念を使用します。 それでは、実装を見
-
複素数用のPythonプログラム
正の数の実数の根は常に2つあります。たとえば、x2が25の場合、xは±5です。ただし、x2が-25の場合、実際のルートは存在しません。負の数の平方根は、絶対値の平方根に虚数単位j=√-1を掛けたものです。 したがって、√−25 =√25𝑋−1 =√25×√−1 =5j 複素数は、実数と虚数の成分で構成されます。 x+yjとして表されます。 xとyはどちらも実数です。 Yに虚数単位を掛けると、複素数の虚数部が形成されます。 例:3 + 2j、10-5.5J、9.55 + 2.3j、5.11e-6 + 4j Pythonには、組み込みの複雑なデータ型があります。複素数オブジェクトは、