共有T组数据,每组数据给出2个大整数A,B,计算A*B的结果。

共有T组数据,每组数据给出2个大整数A,B,计算A*B的结果。

以下是使用C++实现的程序代码,可以计算任意两个大整数A、B的乘积并输出结果。基本思路是利用字符串存储大整数,并模拟手工竖式乘法的过程进行计算。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 10005;

int a[MAXN], b[MAXN], res[MAXN * 2];

void mul(char* str1, char* str2, int len1, int len2) {
    // 将两个字符数组转换为整数数组
    for (int i = 0; i < len1; i++) {
        a[i] = str1[len1 - i - 1] - '0';
    }
    for (int i = 0; i < len2; i++) {
        b[i] = str2[len2 - i - 1] - '0';
    }
    // 计算乘积
    memset(res, 0, sizeof(res));
    for (int i = 0; i < len1; i++) {
        for (int j = 0; j < len2; j++) {
            res[i + j] += a[i] * b[j];
        }
    }
    // 处理进位
    int len = len1 + len2 - 1;
    for (int i = 0; i < len; i++) {
        res[i + 1] += res[i] / 10;
        res[i] %= 10;
    }
    // 去除前导零
    while (len > 0 && res[len] == 0) {
        len--;
    }
    // 输出结果
    for (int i = len; i >= 0; i--) {
        cout << res[i];
    }
    cout << endl;
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        char str1[MAXN], str2[MAXN];
        cin >> str1 >> str2;
        int len1 = strlen(str1);
        int len2 = strlen(str2);
        mul(str1, str2, len1, len2);
    }
    return 0;
}

输入格式要求每组数据占一行,每行包含两个字符串A和B,用空格隔开。输出格式为一行一个字符串,即A*B的乘积。以下是一个示例输入输出:

3
12345678901234567890 98765432109876543210
11111 9999999999
123 456

输出:

1219326311370217956224344941939086200
111109999989
56088

https://blog.csdn.net/songbai1997/article/details/81838741