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. SpringMVC构建Restful。

    因为spring是依赖jackson来生成json,需要添加jar包. pom.xml文件添加依赖. <dependency> <groupId>org.codehaus.ja ...

  2. Charles安装破解及使用

    摘要 在发开过程中,追踪请求和监控请求与返回数据是我们经常会需要的一个需求,在Mac端,Charles是一款非常易用的抓包工具. Mac端的优秀抓包工具--Charles使用 一.简介 Charles ...

  3. java之 Timer 类的使用以及深入理解

    最近一直在看线程知识,然后看到Timer定时器使用了线程实现的定时功能,于是了解了解: 本文 从Time类的使用和源码分析两个方面讲解: 1---Timer类使用 2---源码分析 1.Time类使用 ...

  4. Natas Wargame Level 19 Writeup(猜测令牌,会话劫持)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAq4AAAEKCAYAAADTmtdjAAAABHNCSVQICAgIfAhkiAAAIABJREFUeF

  5. 《安卓网络编程》之第七篇 java编写Service程序

    import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net. ...

  6. XML文件生成

    import java.io.* ; import org.w3c.dom.* ; import javax.xml.parsers.* ; import javax.xml.transform.* ...

  7. [原创]KVM虚拟化实践记录

    一.KVM简介KVM是开源软件,全称是kernel-based virtual machine(基于内核的虚拟机),是一个开源的系统虚拟化模块,基于硬件的完全虚拟化,不过需要硬件支持(如Intel V ...

  8. Fast data loading from files to R

    Recently we were building a Shiny App in which we had to load data from a very large dataframe. It w ...

  9. 用R语言做数据清理(详细教程)

    数据的清理 如同列夫托尔斯泰所说的那样:“幸福的家庭都是相似的,不幸的家庭各有各的不幸”,糟糕的恶心的数据各有各的糟糕之处,好的数据集都是相似的.一份好的,干净而整洁的数据至少包括以下几个要素: 1. ...

  10. Adobe After Effect CC2017 for Mac

    前段时间学习使用 Lottie 制作炫酷的动画,需要 Adobe After Effect 安装 bodymovin 插件,然后导出动画的 json 文件.尝试安装 AE 工具.网上查找 史蒂芬周的博 ...