洛谷题单3-P1009 [NOIP 1998 普及组] 阶乘之和-python-流程图重构

题目描述

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

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

输入格式

一个正整数 n n n。

输出格式

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

输入输出样例

输入

复制代码
3

输出

复制代码
9

说明/提示

【数据范围】

对于 100 % 100 \% 100% 的数据, 1 ≤ n ≤ 50 1 \le n \le 50 1≤n≤50。

方式

代码

python 复制代码
class Solution:
    @staticmethod
    def oi_input():
        """从标准输入读取数据"""
        num = int(input())
        return num

    @staticmethod
    def oi_test():
        """提供测试数据"""
        return 3

    @staticmethod
    def solution(num):
        t, sum = 1, 0

        for i in range(1, num + 1):
            t *= i
            sum += t

        print(sum)

oi_input = Solution.oi_input
oi_test = Solution.oi_test
solution = Solution.solution

if __name__ == '__main__':
    num = oi_test()
    # num = oi_input()
    solution(num)

流程图

是 否 开始 初始化current_factorial=1, total_sum=0 循环控制 i=1 到 num current_factorial *= i total_sum += current_factorial 输出total_sum 结束

相关推荐
会员源码网3 小时前
使用`mysql_*`废弃函数(PHP7+完全移除,导致代码无法运行)
后端·算法
木心月转码ing4 小时前
Hot100-Day10-T438T438找到字符串中所有字母异位词
算法
HelloReader4 小时前
Wi-Fi CSI 感知技术用无线信号“看见“室内的人
算法
颜酱7 小时前
二叉树分解问题思路解题模式
javascript·后端·算法
zone77397 小时前
001:简单 RAG 入门
后端·python·面试
F_Quant7 小时前
🚀 Python打包踩坑指南:彻底解决 Nuitka --onefile 配置文件丢失与重启报错问题
python·操作系统
允许部分打工人先富起来8 小时前
在node项目中执行python脚本
前端·python·node.js
IVEN_8 小时前
Python OpenCV: RGB三色识别的最佳工程实践
python·opencv
qianpeng8978 小时前
水声匹配场定位原理及实验
算法
haosend9 小时前
AI时代,传统网络运维人员的转型指南
python·数据网络·网络自动化