1 条题解
-
0
【本题解由站长编写】
高精度乘法:把结果开成 a.size()+b.size() 的数组,对 a 的每一位去乘 b 的每一位并累加到对应位置(每位累加完所有乘积后统一进位),最后从高位到低位输出。同样先处理符号。
代码(C++17)
#include <bits/stdc++.h> using namespace std; string mulAbs(const string &a, const string &b) { vector<int> res(a.size() + b.size(), 0); for (int i = (int)a.size() - 1, ia = 0; i >= 0; i--, ia++) { for (int j = (int)b.size() - 1, jb = 0; j >= 0; j--, jb++) { res[ia + jb] += (a[i] - '0') * (b[j] - '0'); } } for (int i = 0; i + 1 < (int)res.size(); i++) { res[i + 1] += res[i] / 10; res[i] %= 10; } string out; for (int i = (int)res.size() - 1; i >= 0; i--) out.push_back(char('0' + res[i])); while (out.size() > 1 && out[0] == '0') out.erase(out.begin()); return out; } int main() { string a, b; cin >> a >> b; bool na = a[0] == '-', nb = b[0] == '-'; if (na) a = a.substr(1); if (nb) b = b.substr(1); string res = mulAbs(a, b); if (na != nb && res != "0") res = "-" + res; cout << res << endl; return 0; }
- 1
信息
- ID
- 4240
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 6
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者