leetcode 537. Complex Number Multiplication 复数乘法 + C++中stringstream很好示例
2017-12-17 14:31
411 查看
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:
The input strings will not have extra blank.
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.
本题意很简单,就是计算复数的乘法,
这道题最大的启发就是C++的stringstream真的很好用
建议和这一道题leetcode 539. Minimum Time Difference C++中的stringstream真的很好用 一起学习
代码如下:
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:
The input strings will not have extra blank.
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.
本题意很简单,就是计算复数的乘法,
这道题最大的启发就是C++的stringstream真的很好用
建议和这一道题leetcode 539. Minimum Time Difference C++中的stringstream真的很好用 一起学习
代码如下:
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <numeric> #include <cmath> using namespace std; class Solution { public: string complexNumberMultiply(string a, string b) { int ra, ia, rb, ib; char buff; stringstream aa(a), bb(b), ans; aa >> ra >> buff >> ia >> buff; bb >> rb >> buff >> ib >> buff; ans << ra*rb - ia*ib << "+" << ra*ib + rb*ia << "i"; return ans.str(); } };
相关文章推荐
- LeetCode 537. Complex Number Multiplication (复数乘法,string转int)
- leetcode 592. Fraction Addition and Subtraction 分数加法+C++stringstream的一个很好的示例
- Leetcode刷题记录:计算复数乘法
- leetcode 539. Minimum Time Difference 时间最小差值 + C++中stringstream一个很好示例
- PAT乙级1051. 复数乘法 (15)
- 复数乘法运算(三次实数乘法)-c++代码实现及运行实例结果
- 1051. 复数乘法 (15)
- 1051. 复数乘法 (15)
- 复数乘法的交换律、结合律及乘法 对加法的分配律证明过程
- PAT-B 1051. 复数乘法
- 1051. 复数乘法 (15) PAT 乙级
- PAT (Basic Level) Practise (中文) 1051. 复数乘法 (15)
- leetcode 164. Maximum Gap 相邻元素的最大间隔 + 一个很好的桶排序示范
- 复数乘法运算(三次实数乘法)-c++代码实现及运行实例结果
- 1051. 复数乘法 (15)(C++)
- 一个很好的MyBatis入门示例程序
- PAT 1051. 复数乘法 (15)--浮点数比较时的精度问题
- python-序列(字符串)乘法示例
- pthread多线程加速示例(大型矩阵乘法):Blocking,1024线程^_^
- leetcode -- 537. Complex Number Multiplication【字符串解析 + 复数相乘计算机实现】