python90分 精简版代码 求时间优化处理

P2669 [NOIP2015 普及组] 金币

xiaobaiwww @ 2022-05-15 22:25:20


n = int(input())
sum = 0
ls = []
for i in range(1,n+1):
    for j in range (1,i+1):
        ls.append(i)
for k in range(n):
    sum = sum + ls[k]
print(sum)

by xiaobaiwww @ 2022-05-15 22:29:34

思路就是先输出所有的数字到列表 只要将输入的数字根据列表中循环加上输出就可以 但是时间优化上可能不是很好 超过一万的时候时间在1.4秒左右


by Xeqwq @ 2022-05-15 22:57:45

@xiaobaiwww 本地输出 打表


by liusanrong @ 2022-06-09 10:54:57

k = int(input())

a = 1
i = 1

while a <= k:
    a += i
    i += 1

sum_v = 0
for j in range(1, i-1):
    sum_v += j*j

sum_v += (k-(a-(i-1))+1)*(i-1)
print(sum_v)

by __mcr130102__ @ 2022-12-22 19:06:37

k = int(input())

a = 1

i = 1

while a <= k:

a += i
i += 1

sum_v = 0

for j in range(1, i-1):

sum_v += j*j

sum_v += (k-(a-(i-1))+1)*(i-1)

print(sum_v)


|