标题:n用高精度计算出S=1!+2!+3!+…+n!(n≤50),其中“!”表示阶乘,例如:5!=5 ...
只看楼主
y13
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2021-3-14
 问题点数:0 回复次数:1 
n用高精度计算出S=1!+2!+3!+…+n!(n≤50),其中“!”表示阶乘,例如:5!=5×4×3×2×1。 输入正整数n,输出计算结果S。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
int a[1010000]={},s[1010000]={};
int jc(int a[],int n)
{
    memset(a,0,sizeof(a));
    a[0]=1;
    a[1]=1;
    int x;
    for(int j=2;j<=n;j++)
    {
        for(int i=1;i<=a[0];i++)
        {
        a[i]*=j;
        }
            for(int i=1;i<=a[0];i++)
            {
            if(a[i]>=10)
            {
                a[i+1]+=a[i]/10;
                a[i]%=10;
                if(i==a[0])a[0]++;
            }
        }
    }
    return 0;
}
int jiafa(int s[],int a[])
{
    for(int i=1;i<=s[0];i++)
    {
        s[i]+=a[i];
        if(s[i]>9)
        {
            s[i+1]+=s[i]/10;
            s[i]%=10;
        }
    }
    if(s[s[0]+1]!=0)s[0]++;
}
int bijiao(int s[],int a[])
{
    if(s[0]>=a[0])return 0;
    else return 1;
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        jc(a,i);
        if(bijiao(s,a))s[0]=a[0];
        jiafa(s,a);
    }
    for(int i=s[0];i>=1;i--)cout<<s[i];
}
搜索更多相关主题的帖子: 计算 i++ for int include 
2021-03-14 15:36
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
你这是秀代码,还是提问。提问的话,你的问题是什么?

以下代码随手瞎写的,仅供参考
程序代码:
#include <iostream>
#include <string>
using namespace std;

struct foo
{
public:
    foo( unsigned n ) : data_(1,n)
    {
    }
    foo& operator*=( unsigned n )
    {
        unsigned carry = 0;
        for( auto& c : data_)
        {
            carry += c*n;
            c = carry%10;
            carry /= 10;
        }
        if( carry != 0 )
            data_.push_back( carry );
        return *this;
    }
    foo& operator+=( const foo& b )
    {
        data_.resize( std::max(data_.size(),b.data_.size()) );

        unsigned carry = 0;
        for( size_t i=0; i!=b.data_.size(); ++i )
        {
            carry += data_[i] + b.data_[i];
            data_[i] = carry%10;
            carry /= 10;
        }
        if( carry != 0 )
            data_.push_back( carry );
        return *this;
    }
private:
    string data_;

    friend std::ostream& operator<<( std::ostream& os, const foo& f )
    {
        for( auto itor=f.data_.crbegin(); itor!=f.data_.crend(); ++itor )
            os << (unsigned)(*itor);
        return os;
    }
};

foo S( unsigned n )
{
    foo sum = 0;
    foo factorial = 1;
    for( unsigned i=0; i!=n; ++i )
    {
        factorial *= i+1;
        sum += factorial;
    }
    return sum;
}

int main( void )
{
    cout << S(1) << endl;
    cout << S(2) << endl;
    cout << S(3) << endl;
    cout << S(50) << endl;
}
2021-03-15 09:57



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-505130-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.174727 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved