题解 | #大数乘法#
大数乘法
https://www.nowcoder.com/practice/6b668316f0ac4421ae86d7ead4301b42
#include <iostream> #include <cstring> #include <string> #include<algorithm> using namespace std; const int N = 100010; int a[N], b[N], c[2 * N]; string s1, s2; int main() { cin >> s1 >> s2; string str; int len1 = s1.size(), len2 = s2.size(); if (s1[0] == '-') { for (int i = 1; i < len1; i++) a[i - 1] = s1[len1 - i] - '0'; len1--; } else { for (int i = 0; i < len1; i++) a[i] = s1[len1 - i - 1] - '0'; } if (s2[0] == '-') { for (int i = 1; i < len2; i++) b[i - 1] = s2[len2 - i] - '0'; len2--; } else { for (int i = 0; i < len2; i++) b[i] = s2[len2 - i - 1] - '0'; } for (int i = 0; i < len1; i++) for (int j = 0; j < len2; j++) c[i + j] += a[i] * b[j]; int k = 0; while (c[k] || k < len1 + len2 - 1) { c[k + 1] += c[k] / 10; c[k] %= 10; k++; } while (k > 0 && !c[k]) k--; if ((s1[0] == '-' && s2[0] != '-') || (s1[0] != '-' && s2[0] == '-')) cout << '-'; for (int i = k; i >= 0; i--) str.push_back(c[i] + '0'); cout << str; return 0; }