首页 > 其他 > 详细

腾讯模拟题--大数运算

时间:2016-03-27 12:40:46      阅读:132      评论:0      收藏:0      [点我收藏+]

技术分享

实现方案:

两层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);
}

 

腾讯模拟题--大数运算

原文:http://www.cnblogs.com/lang5230/p/5325309.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!