菜鸟一只,python不行RE

P1009 [NOIP1998 普及组] 阶乘之和

Python_enjoy @ 2023-11-05 10:22:52

def jc(n):
    ans=1;
    for i in range(n):
        ans=ans*i
sss=int(input())
print((1+jc(sss))*sss/2)

by 回声之歌 @ 2023-11-05 10:30:09

def jc(n):
    ans=1;
    for i in range(1,n+1):
        ans=ans*i
    return ans
sss=int(input())
S=0
for i in range(1,sss+1):
    S+=jc(i)
print(S)

by Python_enjoy @ 2023-11-05 10:31:35

@回声之歌 谢谢


by Operation0701 @ 2023-11-09 22:00:17

n = int(input()) dp = [0]n dp[0] = 1 for i in range(n-1): dp[i+1] = dp[i](i+2) print(sum(dp))


by Winds_Land @ 2024-01-24 12:05:49

n = int(input())
lst = [1]
for i in range(1,n+1):
    temp = lst[i-1] * i
    lst.append(temp)
print(sum(lst)-1)

将1到n的阶乘存储到lst[1:n+1]中,每一项可以由前一项求得,避免了重复计算。最后再将阶乘加起来


|