当前位置: 首页 > news >正文

洛谷千题详解 | P1009 [NOIP1998 普及组] 阶乘之和【C++、Java、Python、Pascal语言】

博主主页:Yu·仙笙

专栏地址:洛谷千题详解

目录

题目描述

输入格式

输出格式

输入输出样例

解析: 

C++源码:

Python源码:

Java源码:

Pascal源码:


 -------------------------------------------------------------------------------------------------------------------------------

 -------------------------------------------------------------------------------------------------------------------------------

题目描述

用高精度计算出 S=1!+2!+3!+⋯+n!(n≤50)。

其中 ! 表示阶乘,定义为n!=n×(n−1)×(n−2)×⋯×1。例如,5!=5×4×3×2×1=120。

 -------------------------------------------------------------------------------------------------------------------------------

输入格式

一个正整数 n。

 -------------------------------------------------------------------------------------------------------------------------------

输出格式

一个正整数 S,表示计算结果。

 -------------------------------------------------------------------------------------------------------------------------------

输入输出样例

输入 #1

3

输出 #1

9

 -------------------------------------------------------------------------------------------------------------------------------

解析: 

 思路就是高精乘+高精加,就是把高精乘的模板套上去接着套高精加的模板,b=c=i的阶乘。

 -------------------------------------------------------------------------------------------------------------------------------

C++源码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
struct fantastic     //嗯,开始重载了
{
    int len,s[9999];
    fantastic()
    {
        memset(s,0,sizeof(s));
        len=1;
    }
    fantastic operator=(const char*num)
    {
        len=strlen(num);
        for(int i=0;i<len;++i)
            s[i]=num[len-i-1]-'0';
        return *this;
    }
    fantastic operator=(const int num)
    {
        char a[9999];
        sprintf(a,"%d",num);
        *this=a;
        return *this;
    }
    fantastic (const int num)
    {
        *this=num;
    }
    fantastic (const char * num)
    {
        *this=num;
    }
    fantastic operator+(const fantastic &a)   //这里在重载 “+” 的运算
    {
        fantastic c;
        c.len=max(len,a.len)+1;                //这里就是我们熟悉的竖式模拟了
        for(int i=0,x=0;i<c.len;++i)
        {
            c.s[i]=s[i]+a.s[i]+x;
            x=c.s[i]/10;
            c.s[i]=c.s[i]%10;
        }
        if(c.s[c.len-1]==0)
            --c.len;
        return c;
    }
    fantastic operator * (const fantastic &x)           //然后再来波 “*” 的运算
    {
        fantastic c;
        c.len=len+x.len;                 //又是我们熟悉的竖式模拟
        for(int i=0;i<len;++i)
            for(int j=0;j<x.len;++j)
            {
                c.s[i+j]+=s[i]*x.s[j];
                c.s[i+j+1]+=c.s[i+j]/10;
                c.s[i+j]%=10;
            }
        if(c.s[c.len-1]==0)
            --c.len;
        return c;
    }
};
ostream& operator<<(ostream &out,const fantastic& x)   //重载一下输出
{
    for(int i=x.len-1;i>=0;--i)
        cout<<x.s[i];
    return out;
}
istream& operator>>(istream &in,fantastic &x)       //重载一下输入
{
    char num[9999];
    in>>num;
    x=num;
    return in;
}
int main()         //然后就可以愉快的开始主程序啦
{
    int n;
    fantastic ans=0,num=1;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        num=num*i;
        ans=ans+num;
    }
    cout<<ans<<endl;
}                                         //非常的简单明了

 -------------------------------------------------------------------------------------------------------------------------------

Python源码:

print(reduce(lambda x,y:x+y,[reduce(lambda x,y:x*y,range(1,i+1)) for i in range(1, int(raw_input())+1)]))

 -------------------------------------------------------------------------------------------------------------------------------

Java源码:

    private static final BigInteger[] INTEGERS = new BigInteger[51];
 
    static {
        INTEGERS[0] = new BigInteger("1");
    }
 
    public static void main(String[] args) {
        BigInteger result = new BigInteger("0");
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 1; i <= n; ++ i) {
            INTEGERS[i] = INTEGERS[i - 1].multiply(new BigInteger(String.valueOf(i)));
            result = result.add(INTEGERS[i]);
        }
        System.out.println(result);
    }

 -------------------------------------------------------------------------------------------------------------------------------

Pascal源码:

var a,b:array[1..1000] of int64;
    i,j,la,lb,n:longint;
procedure cheng(t:longint);//高精度乘单精度
var i:longint;
begin
    for i:=1 to la do
        a[i]:=a[i]*t;
    for i:=1 to la do
    begin
        a[i+1]:=a[i+1]+a[i] div 10;
        a[i]:=a[i] mod 10;
    end;
    while a[la+1]>0 do inc(la);
end;
procedure jia;//高精度加法
var i:longint;
begin
    for i:=1 to la do
    begin
        b[i]:=b[i]+a[i];
        b[i+1]:=b[i+1]+b[i] div 10;
        b[i]:=b[i] mod 10;
    end;
    while b[lb+1]>0 do inc(lb);
end;
begin
    read(n);
    a[1]:=1;
    for i:=1 to n do
    begin
        cheng(i);
        jia;
    end;
    for i:= lb downto 1 do
    write(b[i]);
end.

 -------------------------------------------------------------------------------------------------------------------------------

相关文章:

  • 毕业设计 嵌入式 智能婴儿车
  • Python获取 小黑子 弹幕数据+制作词云分析.........
  • 【C++笔记】第二十五篇 内建函数对象
  • 【C/自定义类型详解】——结构体(struct)、位段、枚举(enum)、联合(union)
  • CSDN 编程竞赛·第八期总结
  • 如何搭建网课搜题公众号
  • MySQL总结
  • 【Web UI自动化测试】Web UI自动化测试之PO篇(全网最全)
  • 什么是跨域
  • 智能优化算法:白鲸优化算法-附代码
  • 【初阶C++】细谈new和delete以及函数与类的模板
  • 【EffectiveC++】让自己习惯C++
  • Python实现websocket接口自动化测试
  • 【Flutter】【widget】Checkbox 和 CheckboxListTile 复选框快速学习一下
  • 二叉搜索树——C++
  • 开发和测试争抢环境?是时候进行多环境建设了
  • 【网络篇】第六篇——网络套接字编程(二)(UDP详解)
  • (最新版2022版)剑指offer之队列 栈题解
  • WinForm应用实战开发指南 - 教你如何实现表头的全选操作?
  • 刷题笔记之三(统计回文+连续最大和+查找组成一个偶数最接近的两个素数+把字符串转换成整数+不要二)
  • “彩虹滑道”项目两男童相撞飞跌出去,景区:工作人员误判导致
  • 安徽安庆市委书记张祥安调研假日经济和旅游安全工作
  • 澎湃读报丨央媒头版头条集中刊发:大国应有的样子
  • 上海浪琴环球马术冠军赛明日启幕!五一假期在这里感受精彩
  • 短剧迷|《权宠》一出,《名不虚传》
  • 净海护渔,中国海警局直属第一局开展伏季休渔普法宣传活动