Complex Number Multiplication

127 篇文章 0 订阅

 Given two strings representing two complex numbers.

You need to return a string representing their multiplication. Note i2 = -1 according to the definition.

Example 1:

Input: "1+1i", "1+1i"
Output: "0+2i"
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.

Example 2:

Input: "1+-1i", "1+-1i"
Output: "0+-2i"
Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.

Note:

  1. The input strings will not have extra blank.
  2. The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.
解析:根据复数相乘原则,计算结果

代码 :


class Solution {
public:
    string complexNumberMultiply(string a, string b) {
        
        string ans;
        int areal,axushu,breal,bxushu;
        areal=0;
        breal=0;
        axushu=0;
        bxushu=0;
        int i=0;
        int flag=0;
        for ( i=0; i<a.size(); i++)
        {
            if (a[i]=='-')
            {
                flag=1;
                continue;
            }
            if (a[i]=='+')
            break;
            areal*=10;
            areal+=(a[i]-'0');
        }
        if (flag)
        areal*=(-1);
        flag=0;
        for (i=i+1; i<a.size(); i++)
        {
            if (a[i]=='i')
            break;
            if (a[i]=='-')
            {
                flag=1;
            }
            else
            {
                   axushu*=10;
            axushu+=(a[i]-'0');
            }
         
        }
        if (flag) axushu=axushu*(-1);
        flag=0;
          for ( i=0; i<b.size(); i++)
        {
            if (b[i]=='-')
            {
                flag=1;
                continue;
            }
            if (b[i]=='+')
            break;
            breal*=10;
            breal+=(b[i]-'0');
        }
        if (flag)
        breal*=(-1);
        flag=0;
        
        for (i=i+1; i<b.size(); i++)
        {
            if (b[i]=='i')
            break;
            if (b[i]=='-')
            {
                flag=1;
            }
            else
            {
                   bxushu*=10;
                 bxushu+=(b[i]-'0');
            }
        }
        if (flag) bxushu=bxushu*(-1);
        int ansshi,ansxushu;
        ansshi=0;
        ansxushu=0;
        ansshi=areal*breal;
        ansshi-=(axushu*bxushu);
        ansxushu=areal*bxushu;
        ansxushu+=(axushu*breal);
        ans="";
        if (ansshi<0)
        {
            ans="-";
            ansshi*=(-1);
        }
        ans+=to_string(ansshi);
        ans+="+";
        if (ansxushu<0)
        {
            ans+="-";
            ansxushu*=(-1);
        }
        ans+=to_string(ansxushu);
        ans+="i";
        return ans;
        
    }
};




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值