提交时间:2024-08-20 22:49:13

运行 ID: 173650

#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; string multiplyLargeNumbers(const string& num1, const string& num2) { int len1 = num1.size(); int len2 = num2.size(); if (len1 == 0 || len2 == 0) return "0"; vector<int> result(len1 + len2, 0); for (int i = len1 - 1; i >= 0; i--) { for (int j = len2 - 1; j >= 0; j--) { int mul = (num1[i] - '0') * (num2[j] - '0'); int p1 = i + j, p2 = i + j + 1; int sum = mul + result[p2]; result[p2] = sum % 10; result[p1] += sum / 10; } } int i = 0; while (i < result.size() - 1 && result[i] == 0) i++; string s = ""; for (; i < result.size(); i++) s += '0' + result[i]; return s; } int main() { string num1, num2; cin >> num1; cin >> num2; string product = multiplyLargeNumbers(num1, num2); cout << product; return 0; }