https://blog.csdn.net/wang_1997/article/details/68241892
#include <stdio.h>
#include <cstring>#define maxn 1000
char str1[maxn];
char str2[maxn];
int ans[2 * maxn];int tmp1[maxn];
int tmp2[maxn];int main()
{
while(~scanf("%s %s", str1, str2))
{
memset(tmp1, 0, sizeof(tmp1));
memset(tmp2, 0, sizeof(tmp2));
memset(ans, 0, sizeof(ans));
int len1 = strlen(str1);
int len2 = strlen(str2);
int j = 0;
for(int i = len1-1; i >= 0; i--)
tmp1[j++] = str1[i]-'0';
j = 0;
for(int i = len2-1; i >= 0; i--)
tmp2[j++] = str2[i]-'0';
for(int i = 0; i < len1; i++)
for(int j = 0; j < len2; j++)
ans[i+j] += tmp1[i] * tmp2[j];
for(int i = 0; i < 2*maxn; i++)
if(ans[i] >= 10)
{
int tmp = ans[i] / 10;
ans[i] = ans[i] % 10;
ans[i+1] += tmp;
}
bool flag = false;
for(int i = 2 * maxn-1; i >= 0; i--)
if(flag)
printf("%d", ans[i]);
else if(ans[i] > 0)
{
printf("%d", ans[i]);
flag = true;
}
if(!flag)
printf("0");
printf("\n");
}
return 0;
}
大数乘法
最新推荐文章于 2022-04-16 22:01:43 发布