#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char buf[100] = { 0 };
int res2[1024] = { 0 };
int rlen = 0;
void calc(int a)
{
static int k = 0;
int i = 0,j = 0,tmp =0;
int res[1024] = { 0 };
for (i = strlen(buf) - 1, j = 0; i >= 0; i--, j++) //逐位相乘
{
tmp = (buf[i]-'0') * a;
res[j + 1] = (res[j] + tmp) / 10;
res[j] = (tmp + res[j]) % 10;
}
j = buf[j + 1] > 0 ? j + 1: j;
for (i = 0; i<j+1 ; i++) //将结果与之前结果相加后在放回
{
res2[i + k+1] += (res2[i + k] + res[i])/10;
res2[i + k] = (res2[i + k] + res[i]) % 10;
}
rlen =(res2[i + k-1] > 0 ? i + k-1 : i + k - 2);
k++;
}
int main()
{
char buf2[1024] = { 0 };
scanf("%s", buf);
scanf("%s", buf2);
for (int i = strlen(buf2) - 1; i >= 0; i--)
calc(buf2[i] - '0');
printf("\n res=");
for (int j = rlen; j >= 0; j--)
printf("%d", res2[j]);
system("pause");
}
大数乘法
最新推荐文章于 2024-07-16 20:11:15 发布