03 翻牌問題

#初始化100張牌 0表示背面 1表示正面

card = [[i,0] for i in range(1,101)]
# print(card)
for j in range(2,101):
    jump = j  # jump 表示跳過的牌數
    pos = j - 1  # pos表示翻動的牌的位置
    print(card[pos][1])
    while pos < len(card):
        card[pos][1] = not card[pos][1]  #這一句是關鍵 card[pos][1]初始化爲0 ,而not card[pos][1]就是ture
        # print(card) 
        pos = pos + jump

for card_save in card:
    if card_save[1] == 0:
        print(card_save[0])

這題不難,關鍵是要想到如何將翻牌這個動作化爲數據語言,

就是這一句:

card[pos][1] = not card[pos][1]  #這一句是關鍵 card[pos][1]初始化爲0 ,而not card[pos][1]就是ture

 

 

 

發佈了53 篇原創文章 · 獲贊 120 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章