a*b高精度数组算法

#include<stdio.h>
#include<string.h>
int main()
{
    int a[40]={0},b[40]={0},c[100]={0},len1=0,len2=0;
    char str1[40],str2[40];
    gets(str1);len1=strlen(str1);
    gets(str2);len2=strlen(str2);
    int i,j;
    for(i=0;i<len1;i++)
    {
        a[i]=str1[len1-i-1]-'0';//由于len1-i=3(first),so需要-1,使其符合要求。
    }
    for(j=0;j<len2;j++)
    {
        b[j]=str2[len2-j-1]-'0';
    }
    for(i=0;i<len1;i++)
    {
        for(j=0;j<len2;j++)
    {
        c[i+j]+=a[i]*b[j];
        if(c[i+j]>=10)
        {
             c[i+j+1]+=c[i+j]/10;
             c[i+j]%=10;
        }
    }
    }
    for(i=len1+len2;i>0;i--)
    {
        if(c[i]!=0)
            break;
    }
    do
    {
        printf("%d",c[i]);
    }
    while(i--);
    printf("
");
    return 0;
}
原文地址:https://www.cnblogs.com/gti2baby/p/10422894.html