50分求助!!!

P1009 [NOIP1998 普及组] 阶乘之和

dongrq_cs @ 2023-04-06 20:44:38

#include <iostream>
using namespace std;
int main(){
    int n,sum = 0,s = 1;
    cin >> n;
    for(int i = 1;i <= n;i++){
        s *= i;
        sum += s;
    }
    cout << sum;
    return 0;
}

by fzh2004 @ 2023-04-06 20:49:26

这个题样例很大,c++的话,结果要用高精度


by fzh2004 @ 2023-04-06 20:49:55

@fzh2004 long long 都不够


by ShenEric @ 2023-04-07 21:10:14

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int a[2000];
int b[2000];
int c[2000];
void p(int *a,int *c)
{
    int jw=0;
    for(int i=1;i<=1000;i++)
    {
        c[i]+=a[i]+jw;
        jw=c[i]/10;
        c[i]%=10;
    }
}
void ch(int *a,int c)
{
    int jw=0;
    for(int i=1;i<=1000;i++)
    {
        a[i]=a[i]*c+jw;
        jw=a[i]/10;
        a[i]%=10;
    }
}
int main()
{
    int n;
    cin>>n;
    a[1]=1;
    for(int i=1;i<=n;i++)
    {
        ch(a,i);
        p(a,c);
    }
    bool flag=0;
    for(int i=1000;i>=1;i--)
    {
        if(c[i]!=0)
        {
            flag=1;
        }
        if(flag)
        {
            cout<<c[i];
        }
    }
}

by xxc123 @ 2023-04-10 22:14:17

求关注

 #include<iostream>
using namespace std;
int a[1001],b[1001];
void add()
{
    int t=0;
    for(int i=1000;i>=1;i--)
    {
        b[i]=b[i]+a[i]+t;
        t=b[i]/10;
        b[i]%=10;
    }
}
void multiplication(int x)
{
    int t=0;
    for(int i=1000;i>=1;i--)
    {
        a[i]=a[i]*x+t;
        t=a[i]/10;
        a[i]%=10;
    }
}
void print()
{
    int flag=0;
    for(int i=1;i<=1000;i++)
    {
        if(b[i]!=0) flag=1;
        if(flag) cout<<b[i];
    }
}
int main()
{
    a[1000]=b[1000]=1;
    int n;
    cin>>n;
    for(int i=2;i<=n;i++)
    {
        multiplication(i);
        add();
    }
    print();
    return 0;
}
从末尾算,a是阶乘积,s是和

by xxc123 @ 2023-04-10 22:14:24

@dongrq_cs


by dongrq_cs @ 2023-04-11 20:47:29

@xxc123 已关! 谢谢!!


|