高精度乘法模
2019-04-13 12:20发布
生成海报
#include
#include
#include
using namespace std;
int multiplicative(int a[], int len1, int b[], int len2, int c[])
{
int ans;
for(int i = 1; i <= len1; i++)
{
ans = 0;
for(int j = 1; j <= len2; j++)
{
c[i+j-1] = c[i+j-1]+ans+a[i]*b[j]; // i, j必须要从1开始,不然i+j-1会等于-1,出错;
ans = (c[i+j-1])/10;
c[i+j-1] %= 10;
}
c[len2+i] = ans;
}
return len1+len2;
}
int main()
{
char a[505], b[505];
int c[505], d[505], e[1010];
scanf("%s%s", a, b);
memset(c, 0, sizeof(c));
memset(d, 0, sizeof(d));
memset(e, 0, sizeof(e));
int len1 = strlen(a), len2 = strlen(b), len3;
for(int i = 1; i <= len1; i++) c[i] = a[len1-i] - '0';//从1开始
for(int i = 1; i <= len2; i++) d[i] = b[len2-i] - '0';
len3 = multiplicative(c, len1, d, len2, e);
int i;
for(i = len3; i > 1; i--) if(e[i]) break;
for(; i >= 1; i--) printf("%d", e[i]);
printf("
");
}
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮