python – 通过滚动6面骰子从起点到达N个空间有多少种可能性?

python – 通过滚动6面骰子从起点到达N个空间有多少种可能性?,第1张

概述我刚刚开始像一周前的 python,现在我被困在关于掷骰子的问题上.这是我朋友昨天寄给我的一个问题,我不知道如何自己解决. 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 我刚刚开始像一周前的 python,现在我被困在关于掷骰子的问题上.这是我朋友昨天寄给我的一个问题,我不知道如何自己解决.

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个空间有多少种可能性?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1195031.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存