C++高精度乘法模板

方法还是与高精度加法的方法差不多,列一个竖式,就可以发现其中的规律了。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 3001
using namespace std ;
int main ( )
{
    char a1 [ N ] , b1 [ N ] ;
    int a [ N ] , b [ N ] , c [ N ] , x ;
    memset ( a , 0 , sizeof ( a ) ) ;
    memset ( b , 0 , sizeof ( b ) ) ;
    memset ( c , 0 , sizeof ( c ) ) ;
    gets ( a1 ) ;
    gets ( b1 ) ;
    int lena = strlen ( a1 ) , lenb = strlen ( b1 ) ;
    for ( int i = 0 ; i < lena ; i ++ ) a [ lena - i ] = a1 [ i ] - 48 ;
    for ( int i = 0 ; i < lenb ; i ++ ) b [ lenb - i ] = b1 [ i ] - 48 ;
    for ( int i = 0 ; i <= lena ; i ++ ) 
    {
        x = 0 ;  //用来存放进位 
        for ( int j = 1 ; j <= lenb ; j ++ )
        {
            c [ i + j - 1 ] += a [ i ] * b [ j ] + x ;  
            x = c [ i + j - 1 ] / 10 ;
            c [ i + j - 1 ] %= 10 ;
        }   
        c [ i + lenb ] = x ;         //进位
    } 
    int lenc = lena + lenb ;
    while ( c [ lenc ] == 0 && lenc > 1 )  lenc -- ;   //删除前导0
    for ( int i = lenc ; i > 0 ; i -- ) printf ( "%d" , c [ i ] ) ;
    return 0 ;
}

相关链接:

C++模板小站:
https://blog.csdn.net/ZJ_MRZ/article/details/80950647

C++快速幂模板:
https://blog.csdn.net/zj_mrz/article/details/80950616

C++高精度加法模板:
https://blog.csdn.net/zj_mrz/article/details/80948327

C++高精度减法模板:
https://blog.csdn.net/zj_mrz/article/details/80965324

转载于:https://www.cnblogs.com/zj-mrz/p/10122474.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值