万进制乘法!?

来源:互联网 发布:百度关键词挖掘软件 编辑:程序博客网 时间:2024/05/17 01:03

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int mod=100000000;
int t,len,len2,l1,l2,alon;
char ch[50002],ch2[50002];
long long num1[7000],num2[7000],ans[15000],wei[10];

void _reset()
{
memset(ch,' ',sizeof(ch));
memset(ch2,' ',sizeof(ch2));
memset(num1,0,sizeof(num1));
memset(num2,0,sizeof(num2));
memset(ans,0,sizeof(ans));
l1=l2=0;
}

void getwei()
{
wei[0]=1;
for(int i=1;i<=8;++i)
wei[i]=wei[i-1]*10;
}

int main()
{
freopen("ab.in","r",stdin);
freopen("ab.out","w",stdout);

scanf("%d",&t);
getwei();
while(t--)
{
_reset();
scanf("%s",&ch);len=strlen(ch);
scanf("%s",&ch2);len2=strlen(ch2);

l1=-1,l2=-1;
int i;
for(i=len-1;i>=0;i-=8)
{
++l1;
for(int j=7;j>=0;--j)
if(i-j>=0)
num1[l1]=num1[l1]*10+(ch[i-j]-'0');
}
for(i=len2-1;i>=0;i-=8)
{
++l2;
for(int j=7;j>=0;--j)
if(i-j>=0)
num2[l2]=num2[l2]*10+(ch2[i-j]-'0');
}

for(int i=0;i<=l1;++i)
{
for(int j=0;j<=l2;++j)
{
ans[i+j]+=num1[i]*num2[j];
ans[i+j+1]+=ans[i+j]/mod;
ans[i+j]%=mod;
}
}
alon=l1+l2;
while(ans[alon]==0 && alon)
--alon;

printf("%I64d",ans[alon]);
for(int i=alon-1;i>=0;--i)
{
int tmp=7,ge=0;
while(ans[i]<wei[tmp] && tmp>=1)
{
printf("0");
--tmp;
}
printf("%I64d",ans[i]);
}
printf("\n");
}
return 0;
}


0 0
原创粉丝点击