Python迴圈題怎麼做?

時間 2021-05-05 20:03:53

1樓:睡到人間煮飯時

forxinrange(10,100):

ifstr(x)[::-1] == str(x):

foryinrange(100,1000):

ifstr(y)[::-1] == str(y):

forzinrange(1000,10000):

ifstr(z)[::-1] == str(z)and(x + y == z):

print(x,y,z)

2樓:莉莉絲程式設計師

#判斷回文數

def is_huiwen(x):

if x>=1 and x<10:

return True

else:

is_f=1

x_t=x

num=0

if x_t%10==0:

is_f=0

if is_f==0:

return False

while x_t!=0:

num=num*10+x_t%10

x_t=x_t/10

if num==x:

return True

else:

return False

#生成(10,100)回文數的列表

list1=[i for i in range(10,100)]j=0while True:

if j>=len(list1):

break

if not(is_huiwen(list1[j])):

del list1[j]

else:

j+=1

#生成(100,1000)回文數的列表

list2=[i for i in range(100,1000)]j=0while True:

if j>=len(list2):

break

if not(is_huiwen(list2[j])):

del list2[j]

else:

j+=1

#生成(1000,10000)回文數的列表list3=[i for i in range(1000,10000)]

j=0while True:

if j>=len(list3):

break

if not(is_huiwen(list3[j])):

del list3[j]

else:

j+=1

#得到三個數的結果

k=1while k:

o = random.sample(list1,1)p = random.sample(list2,1)q = random.sample(list3,1)qian=o

zhong=p

hou=q

math_qian=eval(str(qian[0]))math_zhong=eval(str(zhong[0]))math_hou=eval(str(hou[0]))k=math_qian+math_zhong-math_houprint V,sx,11662

3樓:捏爆大臉貓吸乾大臉能

set_1, set_2, set_3 = set(), set(), set()

for i in range(10, 100):

a = str(i)

if a == a[::-1set_1.add(i)for i in range(100, 1000):

a = str(i)

if a == a[::-1set_2.add(i)for i in range(1000, 10000):

a = str(i)

if a == a[::-1set_3.add(i)def get_num(set_1, set_2, set_3):

for c in set_3for b in set_2for a in set_1if a + b == cprint(a, b, creturn

get_num(set_1, set_2, set_3)

4樓:小籠包

for c in list3:

for b in list2:

for a in list1:

if c==a+b:

print(c,b,a)

由於a+b=c

b一定是900+

c一定不超過1100

可以加兩個if和break減少計算量

這個python實驗報告題怎麼做

冰糖雪梨 我本人不是專業的,給乙個實現方式,如下。如果有錯誤,還請各位批評指正。1 usr bin env python323 List creation 4 circle list chr i for i in range 65,65 26 56 number 1 7 time 0 8 while...

這個python作業怎麼做

BoyDZQ import time 導入庫 s time 2017 10 10 23 40 00 時間字串 s tuple time.strptime s time,Y m d H M S 轉化成時間元組 s stamp time.mktime s tuple 將時間元組轉化成時間戳 print ...

怎麼做2022賀年題?

首先和大部分答主一樣,通過泰勒展開放縮,可以把所要證的問題轉化為 求證 e eeimg 1 為了看上去舒服一點,把次數統一成2021,代表紀念已經過去的2021年,即證 e e eeimg 1 再對這個式子進行初中難度的簡單變形,即證 e eeimg 1 我和其他答主大佬不同,沒有那些 容易想到 顯...