Description
Product
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
12 12 2 222222222222222222222222
Sample Output
144 444444444444444444444444
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
int main()
{
char str1[3000],str2[3000];
int d[3000],f[3000],c[6000];
int len1,len2,len,w,e;
while(scanf("%s",str1)!=EOF)
{
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
memset(f,0,sizeof(f));
scanf("%s",str2);
len1=strlen(str1);
len2=strlen(str2);
if(len1<=len2)
{
len=len2;
}
else
{
len=len1;
}
w=len1;
e=len2;
for(int i=0; i<len; i++)
{
if(len1-1>=0)
{
d[i]=str1[len1-1]-'0';
len1--;
}
else d[i]=0;
if(len2-1>=0)
{
f[i]=str2[len2-1]-'0';
len2--;
}
else f[i]=0;
}
for(int i=0; i<w; i++)
{
for(int j=0; j<e; j++)
{
c[i+j]=c[i+j]+d[i]*f[j];
}
}
for(int i=0; i<4000; i++) //i=w+e;
{
if(c[i]>=10)
{
c[i+1]+=c[i]/10;
c[i]=c[i]%10;
}
}
int flag=0;
for(int i=4000; i>=0; i--) //i=w+e;
{
if(flag||c[i])
{
flag=1;
printf("%d",c[i]);
}
}
if(flag==0)
{
printf("0");
}
printf("\n");
}
return 0;
}