数组乘法(大整数相乘)

题目:Power of Cryptography

解法:https://blog.csdn.net/code_pang/article/details/8263971

题目类型:分治(大概吧,也不是很确定)

知识点:pow()函数的底层运算机制(因为这道题居然居然可以用一个函数就AC掉!!??),数字位数的确定,数组相乘法(即两个超大整数的相乘,只能用数组存储),二分查找法(节省时间,毕竟题目给的时间只有1秒!)

题目:牛课网上的https://www.nowcoder.com/acm/contest/75#question进击吧!阶乘

解法:大数乘法

#include <stdio.h>
#include <iostream>
using namespace std;

// 数组乘法, res_size: 表示有多少位, 返回结果的位数
int multiply(int x, int res[], int res_size) {
    int carry = 0;  // 进位
    for (int i=0; i<res_size; i++) {
        int prod = res[i] * x + carry;   //易错点1:记得加上carry进位值
        res[i] = prod % 10;
        carry  = prod/10;
        printf("carry = %d
",carry);
    }

    while (carry!=0) {
        res[res_size] = carry%10;
        carry = carry/10;
        res_size++;
    }
    printf("res_size = %d
",res_size);
    return res_size;
}

void factorial(int n) {
    int res[36000]; // 10000! 位数不超过36000

    // 初始化
    res[0] = 1;         //易错点2:数组的初始化的值应该为1
    int res_size = 1; // 表示有多少位

    // 计算 n!
    for (int x=2; x<=n; x++) {
        res_size = multiply(x, res, res_size);
    }

    for (int i=res_size-1; i>=0; i--) {
        printf("%d", res[i]);
    }
    cout << endl;
}
int main()
{
    ios::sync_with_stdio(false);

    long long N;
    while(cin >> N)
    {
            factorial(N);
    }
    return 0;
}

题目知识点:数组乘法

原文地址:https://www.cnblogs.com/myxdashuaige/p/9153152.html