高精度乘法

高精度乘法也就是大数乘法, 到数字超过long long后就需要用数组存数了, 此时数的加减乘除就无法直接计算, 今天就写一写乘法运算;

void high_precisionMultiplication(string a, string b)//a, b表示两个因子;
{
    vector<int> factor_a, factor_b, product;
    int len_a=a.size(), len_b=b.size();
    int i=0, j;
    /***将两个因子转化成int数组, 这里用vector***/
    while(i<len_a){
        factor_a.push_back(a[i++]-'0');
    }
    reverse(factor_a.begin(), factor_a.end());
    i=0;
    while(i<len_b){
        factor_b.push_back(b[i++]-'0');
    }
    reverse(factor_b.begin(), factor_b.end());
    /*******************************************/
    /***初始化***/    
    for(i=0; i<len_a+len_b; i++)
        product.push_back(0);
    for(i=0; i<len_a; i++){
        for(j=0; j<len_b; j++){
            product[i+j]+=factor_a[i]*factor_b[j];
        }
    }
    int tem=0, tt;
    for(i=0; i<len_a+len_b; i++){
        tt=(product[i]+tem)/10;
        product[i]=(product[i]+tem)%10;
        tem=tt;
    }
    /***除去前导零***/
    for(i=len_a+len_b-1; i>0; i--)
        if(product[i]==0) product.pop_back();
        else break;
    reverse(product.begin(), product.end());
    int len=product.size();
    /***输出乘积***/      
    for(i=0; i<len; i++)
        cout << product[i];
    cout << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值