1 条题解

  • 0

    注意点都在注释里
    #include<iostream>
    #define N 100010
    using namespace std;
    int a[N], b[N];
    int res1[N], res2[N];
    inline int Mul(int a[], int lena, int b[], int lenb)
    { //和前几题一样的高精度板子
    int c[N] = { 0 };
    int lenc = lena + lenb;
    for (int i = 0; i < lena; i++)
    {
    for (int j = 0; j < lenb; j++)
    {
    c[i + j] += a[i] * b[j];
    c[i + j + 1] += c[i + j] / 10;
    c[i + j] %= 10;
    }
    }
    if (c[lenc - 1] == 0 && lenc > 0)
    lenc--;
    for (int i = 0; i < lenc; i++)
    a[i] = c[i];
    return lenc;
    }
    inline int Input(int x[], int y[], int m, int n, int res[])
    { //封装输入函数并返回高精度乘法处理后的数组长度,将结果存在res1/2数组中
    int len, lena;
    int i = 0;
    while (m != 0)
    {
    x[i] = m % 10;
    y[i] = m % 10;
    m /= 10;
    i++;
    }
    len = lena = i;
    for (int i = 1; i < n; i++)
    lena = Mul(a, lena, b, len);
    for (int i = 0; i < lena; i++)
    res[i] = a[i];
    return lena;
    }
    int main()
    {
    int x1, n1, x2, n2;
    int len1, len2, len;
    cin >> x1 >> n1 >> x2 >> n2;
    len1 = Input(a, b, x1, n1, res1);
    len2 = Input(a, b, x2, n2, res2);
    len = Mul(res1, len1, res2, len2);
    //这里注意计算的最终结果回传到了res1数组中!
    for (int i = len - 1; i >= 0; i--)
    cout << res1[i]; //反向输出结果
    return 0;
    }

  • 1

A11-4 高精度整数之间的乘法

信息

难度
7
分类
(无)
标签
递交数
609
已通过
112
通过率
18%
被复制
7
上传者