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.

思路:

首先将 ‘+’找到,将字符串分成左右两部分,然后分别转换成整数就可以了。

int stringToint(string a,int& real,int& virtu)
{
int i = ;
for (; i < a.size();i++)//找到+号 位置i
{
if (a[i] == '+') break;
} real = atoi(a.substr(, i).c_str());
virtu = atoi(a.substr(i+,a.size()-i-).c_str());
//cout << real << " " << virtu << endl; return ;
}
string complexNumberMultiply(string a, string b)
{
if (a.size() == || b.size() == ) return "";
int real1, virtu1, real2, virtu2,real,virtu;
stringToint(a, real1, virtu1);
stringToint(b, real2, virtu2);
real = real1*real2 - virtu1*virtu2;
virtu = real1*virtu2 + virtu1*real2;
char resu[];
sprintf(resu, "%d+%di", real, virtu);
return resu;
}

[leetcode-537-Complex Number Multiplication]的更多相关文章

  1. LC 537. Complex Number Multiplication

    Given two strings representing two complex numbers. You need to return a string representing their m ...

  2. 【LeetCode】537. Complex Number Multiplication 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...

  3. 537 Complex Number Multiplication 复数乘法

    详见:https://leetcode.com/problems/complex-number-multiplication/description/ C++: class Solution { pu ...

  4. 537. Complex Number Multiplication

    题目大意: 给出a, b两个用字符串表示的虚数,求a*b 题目思路: 偷了个懒,Python3的正则表达式匹配了一下,当然acm里肯定是不行的 class Solution: def complexN ...

  5. LeetCode 537. 复数乘法(Complex Number Multiplication)

    537. 复数乘法 537. Complex Number Multiplication 题目描述 Given two strings representing two complex numbers ...

  6. [LeetCode] Complex Number Multiplication 复数相乘

    Given two strings representing two complex numbers. You need to return a string representing their m ...

  7. LeetCode Complex Number Multiplication

    原题链接在这里:https://leetcode.com/problems/complex-number-multiplication/description/ 题目: Given two strin ...

  8. [Swift]LeetCode537. 复数乘法 | Complex Number Multiplication

    Given two strings representing two complex numbers. You need to return a string representing their m ...

  9. Leetcode 202 Happy Number 弗洛伊德判环解循环

    今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...

  10. Leetcode 137 Single Number II 仅出现一次的数字

    原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...

随机推荐

  1. 开启MongoDB客户端访问控制

    参考官方文档:https://docs.mongodb.org/v2.6/tutorial/enable-authentication/ 基于版本:MongoDB 2.6 概览 在MongoDB数据实 ...

  2. 关于TOE(TCP/IP Offload Engine)

    前言 昨天在电脑上抓取的报文,想进行回话,但是发现全部被设备丢弃了,没有debug信息,查看了抓包文件,原来是IP头和TCP.UDP头的检验和全部为0,也就是TOE(TCP/IP Offload En ...

  3. CSS动画效果的回调

    用纯JS实现动画效果代码量大,计算复杂.因此现在前端页面的动画效果一般都采用CSS来实现. CSS动画实现简单高效,但是在处理动画,控制动画过程上却缺少一些有效手段. 例如我们想在动画效果完成时调用回 ...

  4. 写给Android App开发人员看的Android底层知识(1)

    这个系列的文章一共8篇,我酝酿了很多年,参考了很多资源,查看了很多源码,直到今天把它写出来,也是战战兢兢,生怕什么地方写错了,贻笑大方. (一)引言 早在我还是Android菜鸟的时候,有很多技术我都 ...

  5. 自定义控件pickView

    package com.example.healthembed.util; import java.util.ArrayList; import java.util.List; import java ...

  6. 使用Git初始化项目

    1.在Git官网上点击New repository新建项目: 2.在本地新建一个同名文件: 3.打开git bash切换到新建的文件目录: 4.echo "# myprojectname&q ...

  7. 【转】jQuery Validate验证框架详解

    jQuery校验官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一.导入js库 <script type=& ...

  8. 六、 从Controller中访问模板数据(ASP.NET MVC5 系列)

    在这一章节中,我们将创建一个新的MoviesController类,写代码获取movie数据并用视图模板将它们显示到浏览器中. 在我们进行下一操作之前先Build the application.如果 ...

  9. linux不需要密码ssh登陆

    1. 自动ssh/scp方法A为本地主机(即用于控制其他主机的机器) ;B为远程主机(即被控制的机器Server), 假如ip为192.168.60.110;A和B的系统都是Linux在A上运行命令: ...

  10. Spring学习笔记——02 Bean的命名及实例化

    一.Bean的命名 前一篇讲到IoC是一个管理Bean的容器,Bean多数情况下都是通过XML文件进行配置的,其中Bean的命名有以下几种方式,现在梳理一下. 1. 不指定id,只配置类名 <b ...