洛谷题单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 结束

相关推荐
仰泳的熊猫几秒前
LeetCode:51. N 皇后
数据结构·c++·算法·leetcode
独自破碎E1 分钟前
LeetCode 381: O(1) 时间插入、删除和获取随机元素 - 允许重复
java·算法·leetcode
workflower16 分钟前
基本作业-管理⾃⼰的源代码
开发语言·单元测试·软件工程·需求分析·个人开发
a20063801224 分钟前
ply(python版本的flex/bison or Lex/Yacc)
python
froginwe1125 分钟前
Pandas DataFrame:深入理解数据分析的利器
开发语言
Miraitowa_cheems26 分钟前
LeetCode算法日记 - Day 81: 最大子数组和
java·数据结构·算法·leetcode·决策树·职场和发展·深度优先
Jm_洋洋28 分钟前
【Linux系统编程】程序替换:execve(execl、execlp、execle、execv、execvp、execvpe)
linux·运维·c语言·开发语言·程序人生
wokaoyan198131 分钟前
逻辑推演题——谁是骗子
python
九年义务漏网鲨鱼31 分钟前
利用AI大模型重构陈旧代码库 (Refactoring Legacy Codebase with AI)
python
滑水滑成滑头44 分钟前
**标题:发散创新:智能交通系统的深度探究与实现**摘要:本文将详细
java·人工智能·python