AcWing 793. 高精度乘法-28ms-c++
原题链接
简单
作者:
二月
,
2019-10-31 10:12:35
,
所有人可见
,
阅读 1191
使用string存储大数与结果,并进行处理
时间复杂度o(n)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
// res = A * B
string mul(const string& A, const int b)
{
string res;
for (int i = A.size() - 1, t = 0; i >= 0 || t > 0; i--)
{
if (i >= 0) t += (A[i] - '0') * b;
res += (t % 10) + '0';
t /= 10;
}
reverse(res.begin(), res.end());
return res;
}
int main ()
{
ios::sync_with_stdio(false);
string A;
int b;
cin >> A >> b;
cout << mul(A, b) << endl;
return 0;
}
二月,你好。 你的这个方法有一个小问题,当b是0,a不只一位数的时候,结果会有多个0,所以在翻转前得将末尾的0去掉。
b== 0直接return 0就行了吧