我想知道是否可以使用python中的列表来解决Josepheus问题。
简单来说,约瑟夫斯问题就是找到一个圆形排列的位置,如果使用事先已知的跳过参数来处理执行,这将是安全的。
例如:给定循环安排,例如[1,2,3,4,5,6,7]
和跳过参数3,人员将按照3,6,2,7,5,1
的顺序执行,而职位4
将是安全的。
我一直试图使用列表解决这个问题一段时间了,但索引位置对我来说变得棘手。
a=[x for x in range(1,11)]
skip=2
step=2
while (len(a)!=1):
value=a[step-1]
a.remove(value)
n=len(a)
step=step+skip
large=max(a)
if step>=n:
diff=abs(large-value)
step=diff%skip
print a
使用代码段更新了问题,但我不认为我的逻辑是正确的。
答案 0 :(得分:14)
很简单,你可以使用list.pop(i)
删除每个受害者(并获取他的ID)。然后,我们只需要担心包装索引,你只需将跳过的索引mod作为剩余囚犯的数量即可。
那么,问题解决方案变为
def josephus(ls, skip):
skip -= 1 # pop automatically skips the dead guy
idx = skip
while len(ls) > 1:
print ls.pop(idx) # kill prisoner at idx
idx = (idx + skip) % len(ls)
print 'survivor: ', ls[0]
测试输出:
>>> josephus([1,2,3,4,5,6,7], 3)
3
6
2
7
5
1
survivor: 4
答案 1 :(得分:2)
In [96]: def josephus(ls, skip):
...: from collections import deque
...: d = deque(ls)
...: while len(d)>1:
...: d.rotate(-skip)
...: print(d.pop())
...: print('survivor:' , d.pop())
...:
In [97]: josephus([1,2,3,4,5,6,7], 3)
3
6
2
7
5
1
survivor: 4
如果您不想计算索引,可以使用deque
数据结构。
答案 2 :(得分:1)
看起来更糟,但对于初学者来说更容易理解
np.pad(img, ((row,row), (col,col)), mode)
答案 3 :(得分:1)
我的解决方案使用了我在此处网上找到的数学技巧:https://www.youtube.com/watch?v=uCsD3ZGzMgE 它使用二进制方式写圈中的人数和幸存者的位置。结果相同,代码更短。
代码是这样的:
numar_persoane = int(input("How many people are in the circle?\n")) #here we manually insert the number of people in the circle
x='{0:08b}'.format(int(numar_persoane)) #here we convert to binary
m=list(x) #here we transform it into a list
for i in range(0,len(m)): #here we remove the first '1' and append to the same list
m.remove('1')
m.append('1')
break
w=''.join(m) #here we make it a string again
print("The survivor sits in position",int(w, 2)) #int(w, 2) makes our string a decimal number
答案 4 :(得分:0)
这是我对你问题的解决方案:
# simple queue implementation<ADT>
class Queue:
def __init__(self):
self.q = []
def enqueue(self,data):
self.q.insert(0,data)
def dequeue(self):
self.q.pop()
def sizeQ(self):
return len(self.q)
def printQ(self):
return self.q
lists = ["Josephus","Mark","Gladiator","Coward"]
to_die = 3
Q = Queue()
# inserting element into Q
for i in lists:
Q.enqueue(i)
# for size > 1
while Q.sizeP() > 1:
for j in range(1,3):
# every third element to be eliminated
Q.enqueue(Q.dequeue())
Q.dequeue()
print(Q.printQ())
答案 5 :(得分:0)
def Last_Person(n):
person = [x for x in range(1,n+1)]
x = 0
c = 1
while len(person) > 1:
if x == len(person) - 1:
print("Round ", c, "- Here's who is left: ", person, "Person ", person[x], "killed person", person[0])
person.pop(0)
x = 0
c = c+1
elif x == len(person) - 2:
print("Round ", c, "- Here's who is left: ", person, "Person ", person[x], "killed person", person[x + 1])
person.pop(x+1)
x = 0
c = c + 1
else:
print("Round ", c, "- Here's who is left: ", person, "Person ", person[x], "killed person", person[x + 1])
person.pop(x + 1)
x = x + 1
c = c + 1
print("Person", person[x], "is the winner")
Last_Person(50)
答案 6 :(得分:0)
人数n和人数k的总数,表示k-1个人被跳过,圈子中第k个人被杀。
def josephus(n, k):
if (n == 1):
return 1
else:
return (josephus(n - 1, k) + k-1) % n + 1
n = 14
k = 2
print("The chosen place is ", josephus(n, k))
答案 7 :(得分:0)
如果您只是在寻找最终结果,这是一个简单的解决方案。
def JosephusProblem(people):
binary = bin(people) # Converting to binary
winner = binary[3:]+binary[2] # as the output looks loke '0b101001'. removing 0b and adding the 1 to the end
print('The winner is',int(winner,2)) #converting the binary back to decimal
如果您要查找此代码背后的数学运算,请查看以下视频: Josephus Problem(youTube)