Questions tagged «termination»

1
此代码终止的机会是什么?
我写了这个Python代码,想知道它有时是否根本不会终止(假设我们有无限的内存/时间并且没有递归深度限制)。 凭直觉,您会认为它会终止,因为在某些时候您必须很幸运,如果它没有终止,您将有无限的时间来获得幸运。另一方面,随着递归深度的增加,您必须变得越来越幸运。 import random def random_tree(): if random.random() < 0.5: return 0 return [random_tree() for _ in range(random.randint(1, 5))] 如果random_tree不总是终止,为什么?终止的机会是什么? 我已经尝试使用来计算的话,其在它的极好的无用任一给出的答案〜0.684124或... 1。P=1−(1−0.5)(1−(P+P2+P3+P4+P5)/5)P=1−(1−0.5)(1−(P+P2+P3+P4+P5)/5)P = 1 - (1 - 0.5)(1 - (P + P^2 + P^3 + P^4 + P^5)/5)0.6841240.6841240.684124111 可能更复杂,但也令我着迷的是,以下情况的终止机会 :P(a,b)P(a,b)P(a, b) def random_tree(a, b): if random.random() < a: return 0 …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.