zoukankan      html  css  js  c++  java
  • 腾讯模拟题--大数运算

    实现方案:

    两层for,内部具体实现为

    临时结果 = 数1*数2+当前resutl内结果。

    对结果进行解析,保存哪个,下个位置保存哪个

    最后,因为是反向进行保存的,那么就需要翻转工作了。一层for搞定

    直接上代码了

    int result[2048] = { 0 };//题目中给出的。倒是省去了我们纠结如何定义的麻烦
    void Mul(string num1, string num2)
    {
        const char* str1 = num1.c_str();
        const char* str2 = num2.c_str();
        int len1 = num1.size();
        int len2 = num2.size();
        int rol = 0;        //进位偏差
        int resultLen = 0;    //保存最终结果长度,翻转的正确结果
        for (int i = len1 - 1; i >= 0;--i)
        {
            int begin = rol;
            for (int j = len2 - 1; j >= 0; --j)
            {
                int res = (str1[i] - '0') * (str2[j] - '0')+result[begin];
                result[begin] = res % 10;
                result[begin + 1] += res / 10;
                begin++;
                resultLen = begin-1;
            }
            rol++;
        }
        for (int i = 0; i <= resultLen / 2;++i)
        {
            swap(result[i], result[resultLen - i]);
        }
    }
    void TestMul()
    {
        string s1("123123");
        string s2("456");
        Mul(s1, s2);
    }
  • 相关阅读:
    Angular 11 中 Schematics 的代码优化
    GoEasy使用阿里云OSS出现的问题
    易班模拟登录-Day1笔记
    类型别名与接口
    TypeScript中的数据类型
    Javascript类型系统
    手写Promise3
    手写Promise2
    手写Promise1
    Promise基础用法2
  • 原文地址:https://www.cnblogs.com/lang5230/p/5325309.html
Copyright © 2011-2022 走看看