- 高精度乘法
- 2015-08-03 11:13:09 @
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
int a[51000],len;
node()
{
len=0;
memset(a,0,sizeof(a));
}
};
node chengfa(node x1,node x2)
{
node x0;int i,j;
x0.len=x1.len+x2.len-1;
for (i=1;i<=x1.len;i++)
for (j=1;j<=x2.len;j++)
{
x0.a[i+j-1]+=x1.a[i]*x2.a[j];
}
for (i=1;i<=x0.len;i++)
{
x0.a[i+1]+=x0.a[i]/10;
x0.a[i]%=10;
}
i=x0.len;
while (x0.a[i]>10)
{
x0.a[i+1]+=x0.a[i]/10;
x0.a[i]%=10;
i++;
}
x0.len=i;
return x0;
}
int a[51000],b[51000];
int main()
{
node x,x1,x2;
int k;
char s1[1100],s2[1100];
scanf("%s",s1+1);
k=strlen(s1+1);
for (int i=1;i<=k;i++)
x1.a[i]=s1[k-i+1]-'0';
x1.len=k;
scanf("%s",s2+1);
k=strlen(s2+1);
for (int i=1;i<=k;i++)
x2.a[i]=s2[k-i+1]-'0';
x2.len=k;
x=chengfa(x1,x2);
for (int i=x.len;i>=1;i--)
{
printf("%d",x.a[i]);
}
return 0;
}
1 条评论
-
denverjin LV 7 @ 2015-08-13 10:26:07
#include <cstdio>
#include <cstring>
using namespace std;
struct Bigint
{
int n,a[20005];
Bigint()
{
n = 0;
memset(a,0,sizeof(a));
}
void operator = (const char string[10005])
{
n = strlen(string);
for (int i = 0;i < n;i++)
a[i] = string[n-1-i]-'0';
}
Bigint operator * (const Bigint &x)
{
Bigint t;t.n = n+x.n-1;
for (int i = 0;i < n;i++)
for (int j = 0;j < x.n;j++)
t.a[i+j] += a[i]*x.a[j];
for (int i = 0;i < t.n;i++)
{
t.a[i+1] += t.a[i] / 10;
t.a[i] %= 10;
}
if (t.a[t.n]) t.n++;
return t;
}
};
int main()
{
char x[10005],y[10005];
scanf("%s%s",x,y);
Bigint a,b;
a = x;
b = y;
Bigint ans;
ans = a*b;
for (int i = ans.n-1;i >= 0;i--)
printf("%d",ans.a[i]);
printf("\n");
return 0;
}
- 1