1. 程式人生 > 其它 >史上最全的Android面試題集錦

史上最全的Android面試題集錦

// Problem: P1009 [NOIP1998 普及組] 階乘之和
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1009
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// User: Pannnn

#include <bits/stdc++.h>

using namespace std;

vector<int> mul(vector<int> a, int b) {
    vector<int> res;
    
    int p = 0;
    for (int i = 0; i < a.size() || p != 0; ++i) {
        if (i < a.size()) {
            p += a[i] * b;
        }
        res.push_back(p % 10);
        p /= 10;
    }
    return res;
}

vector<int> sum(vector<int> a, vector<int> b) {
    vector<int> res;
    
    int p = 0;
    for (int i = 0; i < a.size() || i < b.size(); ++i) {
        if (i < a.size()) {
            p += a[i];
        }
        if (i < b.size()) {
            p += b[i];
        }
        res.push_back(p % 10);
        p /= 10;
    }
    if (p) {
        res.push_back(p);
    }
    return res;
}

int main() {
    int n;
    cin >> n;
    vector<int> res;
    vector<int> tRes(1, 1);
    // 高精度乘法與高精度加法
    for (int i = 1; i <= n; ++i) {
        tRes = mul(tRes, i);
        res = sum(tRes, res);
    }
    for (int i = res.size() - 1; i >= 0; --i) {
        cout << res[i];
    }
    cout << endl;
    return 0;
}