数组实现大数相乘

还很粗糙,效率也不太高,有时间再封装到c++类。



//数组自乘测试
void multiply(int a[],int len,int n,int dd)
{
    int LEN=len;
    int N=n;
    //初始化result
    int yy=LEN-1,xx=MAX-1;
    while(yy>=0)
    {
        aa[xx--]=a[yy--];
    }
    for(int i=1;i<N;i++)
    {
        for(int i=0;i<MAX;i++)
            result[i]=0;
        int oo=0;
           
        for(int k=LEN-1;k>=0;k--)
        {   
            //clear temp
            for(int ss=0;ss<MAX;ss++)
                temp[ss]=0;
            int carry=0;
            int bit=0;
            for(int j=MAX-1;j>=0;j--)
            {   
                while(aa[j]*a[k]+carry>=10)
                {
                    int temptemp=aa[j]*a[k]+carry;
                    temp[MAX-1-bit-oo]=(aa[j]*a[k]+carry)%10;
                    bit++;
                    j--;
                    carry=temptemp/10;
                }
                temp[MAX-1-bit-oo]=aa[j]*a[k]+carry;
                carry=0;
                bit++;
            }
            for(int dd=MAX-1;dd>=0;dd--)
            {
                int resultcarry=0;
                while(temp[dd]+result[dd]+resultcarry>=10)
                {
                    result[dd]=(temp[dd]+result[dd]+resultcarry)%10;
                    resultcarry=1;
                    dd--;
                }
                    result[dd]=temp[dd]+result[dd]+resultcarry;
                    resultcarry=0;
            }
            oo++;
        }
        int xx=MAX-1;
        while(xx>=0)
        {
            aa[xx]=result[xx];
            xx--;
        }
    }
    print(dd);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值