• [leetcode] 复数乘法 基础模拟


    题目链接
    在这里插入图片描述

    class Solution {
    public:
        string complexNumberMultiply(string num1, string num2) {
            int shi1 = 0, shi2 = 0;
            int xu1 = 0, xu2 = 0;
            shi1 = getShi(num1);
            shi2 = getShi(num2);
            xu1 = getXu(num1);
            xu2 = getXu(num2);
            string ans = "";
            int shi = shi1 * shi2 - xu1 * xu2;
            int xu = shi1 * xu2 + xu1 * shi2;
            ans += toString(shi) + "+";
            ans += toString(xu) + "i";
            return ans;
        }
        int getShi(string s) {
            int len = s.size();
            int ret = 0;
            int fu = 1;
            for(int i = 0; i < len; i ++) {
                if(s[i] == '-') {
                    fu = -1;
                    continue;
                }
                if(s[i] == '+') break;
                ret = ret * 10 + s[i] - '0';
            }
            return ret * fu;
        }
        int getXu(string s) {
            int ret = 0;
            int len = s.size();
            int flag = 0;
            int fu = 1;
            for(int i = 0; i < len; i ++) {
                if(s[i] == '+') flag = 1;
                if(s[i] == '-' && i != 0) {
                    fu = -1;
                    continue;
                }
                if(s[i] == 'i') break;
                if(flag && isdigit(s[i])) {
                    ret = ret * 10 + s[i] - '0';
                }
            }
            return ret * fu;
        }
        string toString(int n) {
            if(n == 0) {
                string t = "0";
                return t;
            }
            string ret = "";
            int fu = 0;
            if(n < 0) fu = 1, n *= -1;
            while(n) {
                int t = n % 10;
                n /= 10;
                ret.push_back('0' + t);
            }
            reverse(ret.begin(),ret.end());
            if(fu == 1) {
                ret = "-" + ret;
            }
            return ret;
        }
    };
    

    在这里插入图片描述

  • 相关阅读:
    day3-python之函数进阶(三)
    day3-python之函数初识(二)
    day3-python之文件操作(一)
    tomcat
    集群
    nginx
    nginx--zabbix监控status waiting
    zabbix监控之mysql主从状态&mysql主从延迟
    zabbix监控之进程&日志监控
    zabbix监控流程(监控linux上某个文件是否有改动)
  • 原文地址:https://www.cnblogs.com/PushyTao/p/16196734.html
Copyright © 2020-2023  润新知