Calculate A * B.
Input
Each line will contain two integers A and B. Process to end of file.
Note: the length of each integer will not exceed 50000.
Output
For each case, output A * B in one line.
Sample Input
1
2
1000
2
Sample Output
2
2000
代码:
#include<stdio.h>
#include<string>
#include<string.h>
#include<algorithm>
#include<math.h>
#define KK 50010
using namespace std;
char a[KK],b[KK];
long long A[KK],B[KK],ans[KK];
int main()
{
int lena,lenb,len_ans,lenA,lenB,sum,m;
while(gets(a)!=NULL)
{
gets(b);
lena=strlen(a);
lenb=strlen(b);
lenA=lenB=0;
for(int i=lena-1; i>=0;)
{
sum=0;
m=1;
while(i>=0&&m<=100000)
{
sum+=(a[i]-'0')*m;
i--; // 1.把字符型转换成整数型;
m*=10; //2.在每个A【】里存六位数;
}
A[lenA++]=sum;
}
for(int i=lenb-1; i>=0;)
{
sum=0;
m=1;
while(i>=0&&m<=100000)
{
sum+=(b[i]-'0')*m;
i--;
m*=10;
}
B[lenB++]=sum;
}
len_ans=lenA+lenB;
for(int i=0; i<lenA; i++)
for(int j=0; j<lenB; j++)
ans[i+j]+=A[i]*B[j]; //相乘;
for(int i=0; i<len_ans; i++)
{
if(ans[i]>1000000)
{
ans[i+1]+=ans[i]/1000000; //进位;
ans[i]=ans[i]%1000000;
}
}
while(ans[len_ans-1]==0&&len_ans!=0) //清除 数前 多余的0;
len_ans--;
printf("%lld",ans[len_ans-1]);
for(int i=len_ans-2; i>=0; i--)
printf("%06lld",ans[i]);
printf("\n");
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(ans,0,sizeof(ans));
}
}