Python語言,類似第二張圖片的例題,麻煩幫忙看看

uj5u.com熱心網友回復:
# a 歐洲 b大洋洲 c亞洲 d非洲 e 美洲
for a in range(1,6):
for b in range(1,6):
for c in range(1,6):
for d in range(1,6):
for e in range(1,6):
if len({a,b,c,d,e})==5:
p1 = (a==3 and e!=2) or (e==2 and a!=3)
p2 = (c==4 and b!=2) or (b==2 and c!=4)
p3 = (c==1 and d!=5) or (d==5 and c!=1)
p4 = (d==4 and b!=3) or (b==3 and d!=4)
p5 = (a==2 and e!=5) or (e==5 and a!=2)
if p1+p2+p3+p4+p5 == 5:
print("亞洲:",c)
print("歐洲:",a)
print("非洲:",d)
print("美洲:",e)
print("大洋洲:",b)
uj5u.com熱心網友回復:
# 小狗a 小兔b 小貓c 小猴d 小鹿e
for a in range(1,6):
for b in range(1,6):
for c in range(1,6):
for d in range(1,6):
for e in range(1,6):
if len({a,b,c,d,e})==5:
p1 = d<c
p2 = a>e
p3 = b>a and b<d
if p1+p2+p3 == 3:
print(a,b,c,d,e)
uj5u.com熱心網友回復:
ls = ['歐洲','美洲','亞洲','大洋洲','非洲']
for i in range(5):
for j in range(5):
for k in range(5):
for m in range(5):
for n in range(5):
temp = (0,ls[i],ls[j],ls[k],ls[m],ls[n])
if len(temp) == len(set(temp)):
if ((temp[3]=='歐洲') and (temp[2] != '美洲')) or ((temp[2]=='美洲') and (temp[3] != '歐洲')):
if ((temp[4]=='亞洲') and (temp[2] != '大洋洲')) or ((temp[2]== '大洋洲') and (temp[4] != '亞洲')):
if ((temp[1]=='亞洲') and (temp[5] != '非洲')) or ((temp[5]=='非洲') and (temp[1] != '亞洲')):
if ((temp[4]=='非洲') and (temp[3] != '大洋洲')) or ((temp[3]=='大洋洲') and (temp[4] != '非洲')):
if ((temp[2]=='歐洲') and (temp[5] != '美洲')) or ((temp[5]=='美洲') and (temp[2] != '歐洲')):
tu = temp[1:6]
#因為陣列不可變,所以做一個新變數tu
for i in range(5):
print(f'{tu[i]}是{i+1}號')
ls = ['小猴','小狗','小貓','小鹿','小兔']
for i in range(5):
for j in range(5):
for k in range(5):
for m in range(5):
for n in range(5):
temp = (0,ls1[i],ls1[j],ls1[k],ls1[m],ls1[n])
if len(temp) == len(set(temp)):
if (temp[3]=='小猴') or (temp[4]=='小猴') and (temp[1]!='小貓'):
if (temp[2]=='小狗') or (temp[3]=='小狗'):
if (temp[3]=='小兔'):
tu = temp[1:6]
for i in range(5):
print(f'{tu[i]}是第{i+1}名')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/38493.html
上一篇:幫忙看看唄
