数组实现大数相乘

来源:互联网 发布:linux硬件级虚拟机 编辑:程序博客网 时间:2024/05/02 00:12
还很粗糙,效率也不太高,有时间再封装到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);
}


原创粉丝点击