A*B Problem
题目背景
高精度乘法模板题。
题目描述
给出两个非负整数,求它们的乘积。
输入格式
输入共两行,每行一个非负整数。
输出格式
输出一个非负整数表示乘积。
样例 #1
样例输入 #1
1
2
样例输出 #1
2
提示
每个非负整数不超过 1 0 2000 10^{2000} 102000。
问题链接: P1303 A*B Problem
问题分析: 大数问题,不解释。
参考链接: (略)
题记: (略)
AC的C++语言程序如下:
/* P1303 A*B Problem */
#include <iostream>
using namespace std;
const int N = 2000;
int c[N * 2 + 1];
int main()
{
string a, b;
cin >> a >> b;
int lena = a.length(), lenb = b.length(), len = lena + lenb;
for (int i = 0; i < lena; i++)
for (int j = 0; j < lenb; j++)
c[i + j] += (a[lena - 1 - i] - '0') * (b[lenb - 1 - j] - '0');
int carry = 0;
for (int i = 0; i < len; i++) {
c[i] += carry;
carry = c[i] / 10;
c[i] %= 10;
}
if (carry) c[len++] = carry;
while (c[len - 1] == 0 && len > 1) len--;
for (int i = len - 1; i >= 0; i--)
cout << c[i];
return 0;
}