LeetCode OJ-537.Complex Number Multiplication

该博客讨论了LeetCode OJ上的537题——复数乘法。内容包括题目描述、理解及解决方案。重点在于从字符串中解析复数的实部和虚部,并进行乘法运算。建议使用状态机处理字符串解析,以避免复杂的条件判断。
摘要由CSDN通过智能技术生成

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:

  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.

题目理解

​ 简单来说就是求复数乘法运算,本题的重点在于将字符串中复数的实部虚部获取出来。至于实际运算,找关系式就好了。对于获取实部虚部,这里建议使用状态机处理,否则太多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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值