写了2小时的50分代码

P1009 [NOIP1998 普及组] 阶乘之和

遇事不决就选C @ 2022-06-30 23:13:31

#include <bits/stdc++.h>
using namespace std;
long long a[55], n, cnt, f;
void w() {
    long long sum = 10;
    for (int i = 0; i < log10(cnt) + 1; i++) {
        a[i] = cnt / sum % (sum / 10);
        sum = sum * 10;
    }
    f = log10(cnt) + 1;
}
void l(int wel) {
    int sum = a[wel] / 10;
    a[wel] = a[wel] % 10;
    a[wel + 1] += sum;
    f++;
    if (a[wel + 1] >= 10) l(wel + 1);
}
int main() {
//  freopen("test.in", "r", stdin);
//  freopen("test.out", "w", stdout);
    cin >> n;
    if (n < 22) {
        int sum = 1; 
        for (int i = 1; i <= n; i++) {
            sum = sum * i;
            cnt = cnt + sum;
        }
        cout << cnt;
        return 0;
    }
    long long sum1 = 1;
    for (int i = 1; i <= n; i++) {
        if (i == 10) w();
        if (i >= 10) {
            long long sum = 10;
            for (int j = 0; j < log10(i) + 1; j++) {
                a[j] = a[j] * cnt / sum % (sum / 10);
                sum = sum * 10;
                if (a[j] >= 10) l(j);
            }
        }
        else {
            sum1 = sum1 * i;
            cnt = cnt + sum1; 
        }
    }
    for (int i = 0; i <= f; i++) {
        cout << a[f];
    }
    return 0;
}

by 我是人999 @ 2022-07-02 13:41:01

改了一些细节之后过了,代码云剪贴板


by 遇事不决就选C @ 2022-07-02 23:12:21

@我是人999 我不会高精诶,瞎写的


by 遇事不决就选C @ 2022-07-02 23:13:14

@我是人999 《亿些细节》,谢谢大佬过了


上一页 |