递推的基础
Posted pythonbigdata
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了递推的基础相关的知识,希望对你有一定的参考价值。
例1:
def ditui(n,m,cx,cy): x=[1,1,2,2,-1,-1,-2,-2] y=[2,-2,1,-1,2,-2,1,-1] d=[[0 for i in range(n+1)]for j in range(m+1)] f=[[0 for i in range(n+1)]for j in range(m+1)] d[cx][cy]=1 for i in range(len(x)): tx=cx+x[i] ty=cy+y[i] if tx>=0 and ty>=0 and tx<=n and ty<=m: d[tx][ty]=1 for i in range(n+1): for j in range(m+1): f[0][0]=1 if d[i][j]==1: f[i][j]=0 elif i==0: f[i][j]=f[i][j-1] elif j==0: f[i][j]=f[i-1][j] else: f[i][j]=f[i][j-1]+f[i-1][j] return f[n][m] if __name__==‘__main__‘: n,m,cx,cy=map(int,input().split()) print(ditui(n,m,cx,cy))
例2:
def tuqiang(n): ans=[0 for i in range(n+1)] for i in range(n+1): if i==0: ans[i]=0 elif i==1: ans[i]=3 elif i==2: ans[i]=6 elif i==3: ans[i]=6 else: ans[i]=ans[i-1]+2*ans[i-2] return ans[n] if __name__==‘__main__‘: n=int(input()) print(tuqiang(n))
例3:
def yanghui(n,m): ans=[[0 for i in range(n+1)]for j in range(n+1)] for i in range(1,n+1): for j in range(1,i+1): if j==1 or j==i: ans[i][j]=1 else: ans[i][j]=ans[i-1][j-1]+ans[i-1][j] return ans[n][m] if __name__==‘__main__‘: n,m=map(int,input().split()) print(yanghui(n,m))
递推与动态规划的基础
以上是关于递推的基础的主要内容,如果未能解决你的问题,请参考以下文章
课程作业02-1-课后作业1-使用递推的方法用杨辉三角形计算