标签:end stdio.h == wrong 递推 style || 备忘 cin
1、递归求解(直接递归会超时,要用备忘录法)
# include<iostream> # include<stdio.h> #include <map> using namespace std; map<long long,long long> mp; long long DG(long long n,long long m) { if(n==1 || m==1) { return 1; } else { long long k = 33*n+467*n+n*1386430+m+35465*n; if(mp.count(k)) { return mp[k]; } else // { long long value = DG(n-1,m) + DG(n,m-1); //int value = DG(n-1,m) + DG(n,m-1)+2 wrong mp[k] = value; return value; } } } int main() { long long n,m; cin>>n>>m; while(n!=0 || m!=0) { cout<<DG(n+1,m+1)<<endl; cin>>n>>m; mp.clear(); }
return 0; }
2、递推
# include<iostream> # include<stdio.h> using namespace std; long long d[40][40]; int main() { int n,m; int i,j; cin>>n>>m; while(n!=0 || m!=0) { for(i=0;i<=n;i++) { for(j=0;j<=m;j++) { if(i==0||j==0) d[i][j] = 1; else d[i][j] = d[i-1][j] + d[i][j-1]; } } cout<<d[n][m]<<endl; cin>>n>>m; } return 0; }
标签:end stdio.h == wrong 递推 style || 备忘 cin
原文地址:https://www.cnblogs.com/fzuhyj/p/9476270.html