3 条题解

  • 0
    @ 2020-04-14 12:18:47
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int num[100];
    
    //1! + 2! + 3! + 4! + 5! = 1 + 2(1 + 3(1 + 4(1 + 5)));
    
    void factorialSum(int n, int num[]) //求n阶乘和
    {
        num[0] = 1;
        int len = 1;
        for (int i = n; i >= 2; i--)
        {
            int add = 0;
            for (int j = 0; j < len; j++)
            {
                int k = num[j] * i + add;
                if(j == 0)
                    k++;
                num[j] = k % 10;
                add = k / 10;
                if(j == len - 1 && add)
                    len++;
            }
        }
        for (int i = len - 1; i >= 0; i--)
            cout << num[i];
        cout << endl;
    
    }
    
    int main()
    {
        int n;
        cin >> n;
        factorialSum(n, num);
    
        return 0;
    }
    
    
  • 0
    @ 2017-11-27 13:38:59

    基本思路:使用惰性求值来构建一个无限列表,后面项的值依赖于之前的项(实现递推),然后用库函数sum求和。

    main=print.sum.(\n->take (n+1) fac).read=<<getLine
      where fac=0:1:zipWith(*)(tail fac)[2..]
    
  • 0
    @ 2017-09-17 15:20:30

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int Max=10001;
    int a[Max],b[Max],len=1,anslen=1,n;

    void jiecheng(int t)
    {
    for(int i=1;i<=len;i++) a[i]*=t;
    int i=1;
    while((a[i]>9)||(i<len))
    {
    a[i+1]+=a[i]/10;
    a[i]%=10;
    i++;
    }
    len=i;
    }

    void jia()
    {
    for(int i=1;i<=len;++i)
    {
    b[i]+=a[i];
    if(b[i]>9)
    {
    b[i+1]+=b[i]/10;
    b[i]%=10;
    anslen=max(anslen,i+1);

    }
    anslen=max(anslen,i);
    }
    }
    int main ()
    {
    scanf("%d",&n);
    a[len]=1;
    for(int i=1;i<=n;++i) {
    jiecheng(i);jia();
    }
    for(int i=anslen;i>0;--i) cout<<b[i];
    return 0;
    }

  • 1

信息

难度
8
分类
(无)
标签
递交数
27
已通过
5
通过率
19%
上传者