旧代码 - 高精度乘法

来源:互联网 发布:南山软件产业基地 编辑:程序博客网 时间:2024/04/29 23:35
#include <stdio.h>#include <memory.h>#include <stdlib.h>#include <string.h> int     i,j,aLen,bLen,cnt,nCases;int     result[2005];char    a[1001],b[1001];int main(){    scanf("%d",&nCases);    while( nCases-- )    {        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        memset(result,0,sizeof(result));        getchar();        scanf("%s %s",a,b);        aLen = strlen(a), bLen = strlen(b);        for( i=0; i<aLen; ++i )        {            for( j=0; j<bLen; ++j )                result[i+j]+=(a[aLen-i-1]-'0')*(b[bLen-j-1]-'0');        }        for( i=0; i<aLen+bLen; ++i )        {            if( result[i] > 9 )            {                result[i+1] += result[i] / 10;                result[i] %= 10;            }        }        cnt = result[aLen+bLen-1]? aLen+bLen-1 : aLen+bLen-2;        for( i=cnt; i>=0; --i )            printf("%d",result[i]);        printf("\n");    }    //    system("pause");    return 0;}

原创粉丝点击