COMPLEXMULT -Editorial

PROBLEM LINK:

Practice

Author: Kunal Demla
Editorialist: Kunal Demla

DIFFICULTY:

Easy

PREREQUISITES:

Strings, Complex Number

PROBLEM:

Given 2 Complex numbers, output the product.

QUICK EXPLANATION:

We store them separately in integers and then multiply them for the final answer.

EXPLANATION:

We study how the number is stored in the string and use the same to extract out the numbers as integers which are then used to multiply and form the answer.

SOLUTIONS:

Setter's Solution
#include<bits/stdc++.h>
using namespace std;
#define ll long long int

void solve()
{
    ll n=0,m=0,x=0,y=0,i,j,k;
    string s1,s2;
    cin>>s1>>s2;
    int flag1=0,flag2=-1;
    if(s1[0]=='-')
        flag1=1;
    for(i=flag1;i<s1.length();i++){
        if(flag2==-1){
            if(s1[i]<='9'&&s1[i]>='0'){
                n=n*10+(s1[i]-'0');
            }
            else if(s1[i]=='-')
            flag2=1;
            else
            flag2=0;
        }
        else{
            if(s1[i]<='9'&&s1[i]>='0'){
                m=m*10+(s1[i]-'0');
            }
        }
    }
    if(flag1)
    n*=-1;
    if(flag2)
    m*=-1;
    
    flag1=0;flag2=-1;
    if(s2[0]=='-')
        flag1=1;
    for(i=flag1;i<s2.length();i++){
        if(flag2==-1){
            if(s2[i]<='9'&&s2[i]>='0'){
                x=x*10+(s2[i]-'0');
            }
            else if(s2[i]=='-')
            flag2=1;
            else
            flag2=0;
        }
        else{
            if(s2[i]<='9'&&s2[i]>='0'){
                y=y*10+(s2[i]-'0');
            }
        }
    }
    if(flag1)
    x*=-1;
    if(flag2)
    y*=-1;

    int reala,imga;
    reala= (n*x) - (m*y);
    imga= (n*y) + (x*m);
    cout<<reala<<(imga>=0?"+":"")<<imga<<"i";
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

int t=1;
cin>>t;
int n=t;
while(t--)
{
    solve();
    cout<<"\n";
}

cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
return 0;
}