panheng
发表于 2019-8-22 15:40:17
#解题思路:生成1-n的数列,每次取1,2,3,n个数字累乘,将结果写入空列表,最后对列表求和即为所求。
n = int(input('请输入n的值:'))
s1 = #获取1到n各数列
list1 = [] #结果数列
nums = 0 #数字计数
needs = 1 #需求数个数
var = 1 #每项初始值
for each in s1: #逐个取出数字
nums += 1 #计数器+1
if nums == needs: #满足目标数字个数时,加入累乘值,初始化变量
var *= each
list1.append(var)
nums = 0
var = 1
needs += 1
else: #累乘操作
var *= each
if s1.index(each) == n-1:# 如果是最后1个数,累乘后加入list1
list1.append(var)
print(list1,sum(list1))
Geoffreylee
发表于 2020-2-23 22:12:09
from functools import reduce
def nature_num():
n = 0
while True:
n += 1
yield n
def f_31(n):
n_num = nature_num()
length = next(n_num)
sum = 0
temp = []
for i in range(1, n+1):
temp.append(i)
if len(temp) == length:
sum += reduce(lambda x, y: x * y, temp)
length = next(n_num)
temp.clear()
if len(temp) != 0:
sum += reduce(lambda x, y: x * y, temp)
return sum
print(f_31(20))
嘿嘿嘿的黑子哥
发表于 2020-2-28 13:20:53
123
杜若左
发表于 2020-3-29 23:50:57
学习一下
holiday_python
发表于 2020-4-18 13:55:59
的
19971023
发表于 2020-6-3 17:48:57
1
余小c真的很强
发表于 2020-6-3 18:00:23
jerryxjr1220 发表于 2017-4-19 20:35
谢谢分享
aironeng
发表于 2020-12-3 09:10:56
学习
君子好逑
发表于 2022-9-16 11:39:48
from sre_constants import GROUPREF_EXISTS
from tokenize import group
from unittest import result
sum = 0
gropes = []
number = 20
times = 1
while(sum < 16):
gropes.append(times)
sum += times
times += 1
sum -= gropes.pop()
gropes.append(number - sum)
str1 = ""
result = 0
orders = 1
for i in range(0, len(gropes)):
tempt = 1
for i in range(0,gropes):
str1 += str(orders) + "*"
tempt *= orders
orders += 1
str1 = str1[ : -1]
result += tempt
str1 += "+"
str1 = str1[ : -1]
str1 += " = "
print(str1,result)
1106697365
发表于 2022-9-22 16:20:29
Aa