因此,這裏是維基上的Josephus problem。 我遇到的問題是線性變化,但爲了清晰起見,我會重申整個問題。約瑟夫斯益智的線性變化
(數=自然數)
有是消除以下方式號碼的方法:
i=2
while 1:
remove numbers that are *placed* at positions divisible by i
i+=1
同時,也給出了一些K
,你要確認如果這個數字K
將消除其存活。
E.g. (假定索引從0開始)
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 ...
0,1,2,3,4,5,6,7,8, 9,10,11,12,13,14,15 ... (indices)
After step 1 (elimination at i=2)
2,4,6,8,10,12,14,16 ...
0,1,2,3, 4, 5, 6, 7 ... (indices)
After step 2 (elimination at i=3)
2,4,6,10,12,16 ... (8 and 14 got removed cause they were at index 3 and 6 resp.)
0,1,2, 3, 4, 5 ... (indices)
正如我們可以看到2,4,6-是safe
此步驟之後,由於處理將用於消除被選擇更高和更高的值。
因此,再次給出K
如何確定K
將是safe
?
在第一個代碼片段的'while'循環中'i'是否增加? – MAK 2010-11-12 12:39:09
woops :)固定。 – 2010-11-12 13:04:21
您的示例中存在不一致。在你的第一步中(消除2的倍數的位置)消除位置0處的數字。但是在第二步中(消除位置3處的倍數),位置0處的數字仍然存在。你能否澄清這是否是故意的? – 2010-11-12 13:19:55