約瑟夫問題

young joker發表於2020-11-08

約瑟夫問題

list1 = [i for i in range(1,40)]
print(list1)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
k = 0
while len(list1)>2 :
    i = 0
    while i<len(list1):
        k += 1
        if k == 3:      # 遇3殺人 
            list1.remove(list1[i])
            k =0 
        else:
            i +=1
print(list1)
[10, 25]

相關文章