50分,没学高精度

P1009 [NOIP1998 普及组] 阶乘之和

guoyanwei120223 @ 2024-08-10 16:35:58

警示后人:不要这样写

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,a=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        long long b=1;
        for(int j=1;j<=i;j++)
        {
            b*=j;
        }
        a+=b;
    }
    cout<<a<<endl;
}

by littleFond @ 2024-08-18 21:46:06

孩子,你其实可以下载数据


by jywa @ 2024-08-21 15:52:57

你试试这样

#include <iostream>
#include <vector>

using namespace std;

vector<int> multiply(vector<int> a, int b) {
    vector<int> c;
    int t = 0;
    for (int i = 0; i < a.size(); i++) {
        int temp = a[i] * b + t;
        c.push_back(temp % 10);
        t = temp / 10;
    }
    while (t > 0) {
        c.push_back(t % 10);
        t /= 10;
    }
    return c;
}

vector<int> factorial(int n) {
    vector<int> res = {1};
    for (int i = 2; i <= n; i++) {
        res = multiply(res, i);
    }
    return res;
}

vector<int> add(vector<int> a, vector<int> b) {
    vector<int> c;
    int t = 0;
    int i = 0, j = 0;
    while (i < a.size() && j < b.size()) {
        int temp = a[i++] + b[j++] + t;
        c.push_back(temp % 10);
        t = temp / 10;
    }
    while (i < a.size()) {
        int temp = a[i++] + t;
        c.push_back(temp % 10);
        t = temp / 10;
    }
    while (j < b.size()) {
        int temp = b[j++] + t;
        c.push_back(temp % 10);
        t = temp / 10;
    }
    if (t > 0) c.push_back(t);
    return c;
}

int main() {
    int n;
    cin >> n;
    vector<int> sum = {0};
    for (int i = 1; i <= n; i++) {
        sum = add(sum, factorial(i));
    }
    for (int i = sum.size() - 1; i >= 0; i--) {
        cout << sum[i];
    }
    cout << endl;
    return 0;
}

请加入我的龙天工作室 ←点这里


上一页 |