编译不通过

P1009 [NOIP1998 普及组] 阶乘之和

Showball @ 2022-08-03 18:26:30

自己用vs编译通过,样例也过了 ,但是提交洛谷编译失败

#include<iostream>
#include<vector>
using namespace std;
typedef vector<int> VI;
VI mul(VI &A,int b) {//高精度乘法
    int t = 0;
    VI C;
    for (int i = 0; i < A.size()||t; i++) {
        if (i < A.size()) t += A[i] * b;
        C.push_back(t % 10);
        t /= 10;
    }
    while (C.size() > 1 && C.back() == 0) C.pop_back();
    return C;
}
VI add(VI &A, VI &B) {//高精度加法
    if (A.size() < B.size()) return add(B, A);
    int t = 0;
    VI C;
    for (int i = 0; i < A.size(); i++) {
        t += A[i];
        if (i < B.size()) t += B[i];
        C.push_back(t % 10);
        t /= 10;
    }
    if (t) C.push_back(t);
    return C;
}
VI Factorial(int n) {//高精度阶乘
    VI C;
    C.push_back(1);
    for (int i = 2; i <=n; i++) {
        C = mul(C, i);
    }
    return C;
}
int main() {
    int n;
    cin >> n;
    VI C;
    C.push_back(1);
    for (int i = 2; i <=n; i++) {
        C = add(C, Factorial(i));
    }
    for (int i = C.size() - 1; i >= 0; i--) cout << C[i];
    return 0;
}

by 昒昕 @ 2022-08-03 18:33:14

@Scnu_Sea 把 add 函数的 & 去掉


by Showball @ 2022-08-03 18:38:02

@昒昕 ac了诶,感谢!!!是什么原因呢?


by 昒昕 @ 2022-08-03 18:41:27

@Scnu_Sea & 表引用,你 C=add(C,Factorial(i)) 等于引用了一个函数返回值,引用必须引用变量


by Showball @ 2022-08-03 18:42:17

@昒昕 好耶,学到啦,感谢!!!!!!!


|