LeetCode OJ-537.Complex Number Multiplication
来源:互联网 发布:2016年十大网络神曲 编辑:程序博客网 时间:2024/05/17 15:56
LeetCode OJ-537.Complex Number Multiplication
题目描述
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.
题目理解
简单来说就是求复数乘法运算,本题的重点在于将字符串中复数的实部虚部获取出来。至于实际运算,找关系式就好了。对于获取实部虚部,这里建议使用状态机处理,否则太多if-else会比较头疼。获取的关键部分,在代码中给了注释,具体可以参考代码。
Code
// 获取实部虚部void get_real_vir(const string &s, int &real, int &vir){ // 解析字符串时使用的状态机 enum parse_state { ps_start = 0, ps_real_symbol, ps_real, ps_plus, ps_vir_symbol, ps_vir, ps_end } state; state = ps_start; int i, j, k; char sys_real1 = 0, sys_vir1 = 0; // 记录实部虚部的符号,0 + 1 - int real1 = 0, vir1 = 0; j = 0; k = 0; stack<char> real_stack; stack<char> vir_stack; for (i = 0; i < s.length(); ++i) { switch (state) { case ps_start: if (s[i] == '-') { sys_real1 = 1; state = ps_real_symbol; } else { sys_real1 = 0; state = ps_real; real_stack.push(s[i]); // 处理实部虚部一定要做压栈, // 否则多位数的实部虚部无法正确处理 // 直接乘10的j次幂,68会变86 // 因为字符串遍历是从百位十位开始的 // 需要保存各个位置的数再依次处理 } break; case ps_real_symbol: state = ps_real; real_stack.push(s[i]); break; case ps_real: if (s[i] == '+') { while (!real_stack.empty()) { real1 += (real_stack.top() - '0') * std::pow(10, j++); real_stack.pop(); } real1 *= (sys_real1 == 0 ? 1 : -1); state = ps_plus; } else { real_stack.push(s[i]); } break; case ps_plus: if (s[i] == '-') { state = ps_vir_symbol; sys_vir1 = 1; } else { state = ps_vir; vir_stack.push(s[i]); } break; case ps_vir_symbol: vir_stack.push(s[i]); state = ps_vir; break; case ps_vir: if (s[i] == 'i') { state = ps_end; while (!vir_stack.empty()) { vir1 += (vir_stack.top() - '0') * std::pow(10, k++); vir_stack.pop(); } vir1 *= (sys_vir1 == 0 ? 1 : -1); } else { vir_stack.push(s[i]); } break; case ps_end: break; default: break; } } real = real1; vir = vir1;}string complex_number_multiply(const string &s1, const string &s2){ string res; int real1, vir1; get_real_vir(s1, real1, vir1); int real2, vir2; get_real_vir(s2, real2, vir2); int a = real1 * real2; int b = (real1 * vir2 + real2 * vir1); int c = vir1 * vir2; int real = a + (-1) * c; int vir = b; string real_str; stack<char> real_stack; if (real < 0) { real_str.push_back('-'); real *= -1; } else if (real == 0) { real_str += "0"; } while (real > 0) { real_stack.push((real % 10) + '0'); real /= 10; } while (!real_stack.empty()) { real_str.push_back(real_stack.top()); real_stack.pop(); } string vir_str; stack<char> vir_stack; if (vir < 0) { vir_str.push_back('-'); vir *= -1; } else if (vir == 0) { vir_str += "0"; } while (vir > 0) { vir_stack.push((vir % 10) + '0'); vir /= 10; } while (!vir_stack.empty()) { vir_str.push_back(vir_stack.top()); vir_stack.pop(); } res = real_str + "+" + vir_str + "i"; cout << res << endl; return res;}
0 0
- LeetCode OJ-537.Complex Number Multiplication
- Leetcode-537. Complex Number Multiplication
- leetcode 537. Complex Number Multiplication
- LeetCode 537. Complex Number Multiplication
- Leetcode 537. Complex Number Multiplication
- leetcode 537. Complex Number Multiplication
- LeetCode | 537. Complex Number Multiplication
- LeetCode:537. Complex Number Multiplication
- LeetCode----537. Complex Number Multiplication
- [leetcode]537. Complex Number Multiplication
- LeetCode-537. Complex Number Multiplication
- [LeetCode]537. Complex Number Multiplication
- [leetcode]: 537. Complex Number Multiplication
- 【LeetCode】537. Complex Number Multiplication
- LeetCode 537. Complex Number Multiplication
- leetcode-complex number multiplication
- 【LeetCode】537. Complex Number Multiplication【M】
- [LeetCode]537. Complex Number Multiplication<Java>
- AC自动机,Tarjan(ZOJ 3784,String of Infinity)
- 深入理解OAuth2.0协议
- 超简单的okhttp封装工具类(上)
- js正则字符串对象方法
- 计算机组成原理试卷一(期末复习备用)
- LeetCode OJ-537.Complex Number Multiplication
- 解决使用scrapy中 ERROR:Error downloading
- NYOJ116 士兵杀敌(二)(线段树区单点更新,区间求和,zkw线段树)
- Swift3使用GCD和DispatchQueues
- android-Bar系列
- Linux 下mysql遇到的坑
- swipeRefreshLayout基本用法
- 设计模式之桥接模式
- 解决listview item Textview加超链接 超链接以外部分不响应listview item点击事件问题