描述
高精度乘法
输入:两行,每行表示一个非负整数(不超过10000位)
输出:两数的乘积。
样例1
样例输入1
99
101
样例输出1
9999
题解
这道题和之前的Vijos 1010 清帝之惑之乾隆一样是求高精度乘法的题,不同之处是这次是两个大数乘法,之前是一个大数和一个整数范围内的数进行乘法,所以数据的保存方式和处理方式稍微有些区别。具体见代码:)
代码:
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 20020;
char s[maxn], t[maxn], res[maxn], tmp[maxn];
int lenS, lenT;
void preSolve()
{
memset(tmp, 0, sizeof(tmp));
memset(res, 0, sizeof(res));
lenS = strlen(s);
lenT = strlen(t);
// strrev(s);
// strrev(t);
for (int i = 0; i < lenS/2; i ++)
{
char p = s[i];
s[i] = s[lenS-1-i];
s[lenS-1-i] = p;
}
for (int i = 0; i < lenT/2; i ++)
{
char p = t[i];
t[i] = t[lenT-1-i];
t[lenT-1-i] = p;
}
for (int i = 0; i < lenS; i ++)
s[i] -= '0';
for (int i = 0; i < lenT; i ++)
t[i] -= '0';
}
void solve()
{
for (int i = 0; i < lenT; i ++)
{
int a = t[i];
int c = 0;
for (int j = 0; j < lenS + 1; j ++)
{
c += t[i] * s[j];
tmp[j] = c % 10;
c /= 10;
}
c = 0;
for (int j = 0; j < lenS + 1; j ++)
{
c += tmp[j] + res[i+j];
res[i+j] = c % 10;
c /= 10;
}
}
int i = maxn-1;
for (;res[i] == 0 && i > 0; i --);
for (;i >= 0; i--)
printf("%d", res[i]);
puts("");
}
int main()
{
scanf("%s%s", s, t);
preSolve();
solve();
return 0;
}