#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string multiply(string a, string b)
{
int len = a.size() + b.size() + 10;
string ans(len, '0');
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
for (int i = 0; i < a.size(); i++)
{
int t = 0;
for (int j = 0; j < b.size(); j++)
{
t += (ans[i + j] - '0') + (a[i] - '0') * (b[j] - '0');
ans[j + i] = t % 10 + '0';
t /= 10;
if (t && j == b.size() - 1)
{
ans[j + i + 1] = t + '0';
}
}
}
reverse(ans.begin(), ans.end());
return ans.substr(ans.find_first_not_of('0'));
}
int main()
{
string a, b;
cin >> a >> b;
cout << multiply(a, b) << endl;
return 0;
}
C++大数乘法运算
最新推荐文章于 2024-05-08 21:31:31 发布