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. 每天一道Java题[3]

    问题 为什么在重写equals()方法的同时,必须重写hashCode()方法? 解答 在<每天一道Java题[2]>中,已经对hashCode()能否判断两个对象是否相等做出了解释.eq ...

  2. 基本DOS命令之 netstat 命令详解

     netstat 命令(查看端口) netstat 命令用于显示与 IP .TCP .UDP 和 ICMP 协议相关的统计数据,一般用于检验本机各端口的网络连接情况,可以使用 netstat 命令查看 ...

  3. 第三章(附)mysql表类型MyISAM和InnoDB区别(决定了是否支持事务)

    mysql表类型MyISAM和InnoDB区别 MyISAM:这个是默认类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的顺序访问 ...

  4. 开涛spring3(2.1) - IoC基础

    2.1.1  IoC是什么 Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在 ...

  5. rPithon vs. rPython(转)

    Similar to rPython, the rPithon package (http://rpithon.r-forge.r-project.org) allows users to execu ...

  6. uibutton颜色设置

     UIColor *color = [UIColor colorWithRed:100 / 255.0 green:20 / 255.0 blue:50 / 255.0 alpha:1.0]; 

  7. linux下mysql重置密码

    如果忘记mysql的root密码可以采取下面的步骤重新设置 1.kill掉所有mysql的进程 2.使用--skip-grant-tables的参数启动mysql shell> mysqld_s ...

  8. xtrabackup备份原理

    Percona XtraBackup工作原理 Percona XtraBackup是基于InnoDB的崩溃恢复功能.复制InnoDB数据文件,导致内部不一致的数据; 但随后它对文件执行崩溃恢复,使它们 ...

  9. comm的用法

    1.comm的功能 对两个排序过的文本文件进行逐行比较基本用法:comm -[123] file1 file2 2.例子 1)显示两个文本文件都有的行#comm -12 file1 file2 2)显 ...

  10. Spring事务管理—aop:pointcut expression解析

    先来看看这个spring的配置文件的配置: <!-- 事务管理器 --> <bean id="transactionManager"  class="o ...