代码
来源:面试常考 大数加减乘除
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<int> mult(string s1, string s2)
{
int l1 = s1.size();
int l2 = s2.size();
vector<int> res(l1 + l2, 0);
for (int i = 0; i < l1; i++)
{
for (int j = 0; j < l2; j++)
{
res[i + j + 1] += ((s1[i] - '0')*(s2[j] - '0'));
}
}
int x = l1 + l2 - 1;
for (int i = x; i > 0; i--)
{
res[i - 1] += res[i] / 10;
res[i] %= 10;
}
return res;
}
int main()
{
string a = "99999";
string b = "99999";
vector<int> ans = mult(a, b);
int i = 0;
//去除前面的0
while (i < ans.size() && ans[i] == 0)
{
i++;
}
for (; i < ans.size(); i++)
{
cout << ans[i];
}
cout << endl;
system("pause");
return 0;
}