Imagine you are playing a board game. You roll a 6-faced dice and move forward the same number of spaces that you rolled. If the finishing point is “n” spaces away from the starting point,please implement a program that calculates how many possible ways there are to arrive exactly at the finishing point.
所以看起来我将使用带有“N”的参数来创建一个函数,当它到达某个点时,让我们说10,所以我们都可以看到有多少可能性从起点开始到达10个空格.
我想这与“组合”有关,但我不确定它应该如何在python中编码.
拜托,Python大师!
解决方法 这是计算精确结果的一种方法,既不使用迭代也不使用递归:def ways(n): A = 3**(n+6) M = A**6 - A**5 - A**4 - A**3 - A**2 - A - 1 return pow(A,n+6,M) % Afor i in xrange(20): print i,'->',ways(i)
输出与https://oeis.org/A001592一致
0 -> 11 -> 12 -> 23 -> 44 -> 85 -> 166 -> 327 -> 638 -> 1259 -> 24810 -> 49211 -> 97612 -> 193613 -> 384014 -> 761715 -> 1510916 -> 2997017 -> 5944818 -> 11792019 -> 233904总结
以上是内存溢出为你收集整理的python – 通过滚动6面骰子从起点到达N个空间有多少种可能性?全部内容,希望文章能够帮你解决python – 通过滚动6面骰子从起点到达N个空间有多少种可能性?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)