while 1: try: n = input().split() n1 , n2 = int(n[0]) , int(n[1]) c = 0 for i in range(n1,n2+1): word = str(i) box = [] s = 0 for j in word: box.append(j) l = len(box) for k in range(l): s = s + int(box[k]) ** l if s == i: print(i , end = ' ') c = 1 if c == 0: print('none' , end = '') print() except EOFError: break
這是我的Source code
我有兩個問題:
當遇上大數計算時,如何提高Python的運算速度
以及為何使用eval()函數反而會TLE(原本花了0.9s)
while 1: try: n = input().split() n1 , n2 = int(n[0]) , int(n[1]) c = 0 for i in range(n1,n2+1): word = str(i) box = [] s = 0 for j in word: box.append(j) l = len(box) for k in range(l): s = s + int(box[k]) ** l if s == i: print(i , end = ' ') c = 1 if c == 0: print('none' , end = '') print() except EOFError: break
這是我的Source code
我有兩個問題:
當遇上大數計算時,如何提高Python的運算速度
以及為何使用eval()函數反而會TLE(原本花了0.9s)
抱歉程式碼跑掉了
while 1:
try:
n = input().split()
n1 , n2 = int(n[0]) , int(n[1])
c = 0
for i in range(n1,n2+1):
word = str(i)
box = []
s = 0
for j in word:
box.append(j)
l = len(box)
for k in range(l):
s = s + int(box[k]) ** l
if s == i:
print(i , end = ' ')
c = 1
if c == 0:
print('none' , end = '')
print()
except EOFError:
break