Product(大数乘法模板)

来源:互联网 发布:知味观点心实体店 编辑:程序博客网 时间:2024/04/28 00:16
K -Product

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status

Description

Download as PDF


 Product 

The Problem

The problem is to multiply two integers X, Y. (0<=X,Y<10250)

The Input

The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.

The Output

For each input pair of lines the output line should consist one integer the product.

Sample Input

12122222222222222222222222222

Sample Output

144444444444444444444444444
#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int N = 500;int main(){    char a[N],b[N];    while(cin>>a>>b)    {        if(strcmp(a,"0")==0 || strcmp(b,"0")==0)        {            cout<<"0"<<endl;            continue;        }        int s[N*10];        memset(s,0,sizeof(s));        int aa[N],bb[N];        memset(aa,0,sizeof(aa));        memset(bb,0,sizeof(bb));        int lena=strlen(a);        int lenb=strlen(b);        int la=1,lb=1;        for(int i=lena-1; i>=0; i--)            aa[la++]=a[i]-'0';        for(int i=lenb-1; i>=0; i--)            bb[lb++]=b[i]-'0';        for(int i=1; i<la; i++)            for(int j=1; j<lb; j++)            {                s[i+j-1]+= aa[i]*bb[j];            }        for(int i=1; i<=la+lb; i++)        {            s[i+1]+=s[i]/10;            s[i]=s[i]%10;        }        int r=1;        for(int i=la+lb; i>=1; i--)        {            if(s[i]!=0)            {                r=i;                break;            }        }        for(int i=r; i>=1; i--)            cout<<s[i];        cout<<endl;    }    return 0;}


0 0