以下是C++代码实现:

#include #include using namespace std;

const int MAXN = 200005; // 数组大小 int a[MAXN], b[MAXN], c[MAXN]; // 存储结果的数组

void multiply(int a[], int b[], int c[]) { // 高精度乘法函数 int lena = strlen(a), lenb = strlen(b); for (int i = 0; i < lena; i++) { for (int j = 0; j < lenb; j++) { c[i+j] += a[i] * b[j]; // 乘法 } } for (int i = 0; i < lena+lenb-1; i++) { // 处理进位 c[i+1] += c[i] / 10; c[i] %= 10; } while (c[lena+lenb-1] == 0 && lena+lenb-1 > 0) lena+lenb--; }

int main() { string stra, strb; cin >> stra >> strb; int lena = stra.length(), lenb = strb.length(); for (int i = 0; i < lena; i++) a[i] = stra[lena-i-1]-'0'; // 字符串转化为数组,并且反转 for (int i = 0; i < lenb; i++) b[i] = strb[lenb-i-1]-'0'; multiply(a, b, c); // 调用高精度乘法函数 int i = lena + lenb - 1; while (i > 0 && c[i] == 0) i--; // 去掉前导0 for (; i >= 0; i--) cout << c[i]; // 输出结果 cout << endl; return 0;

C++本题为高精度乘高精度模板题请同学们完成高精度乘高精度算法

原文地址: https://www.cveoy.top/t/topic/fzdE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录