1. 程式人生 > >HDU 2044(遞推&遞歸_A題)解題報告

HDU 2044(遞推&遞歸_A題)解題報告

多少 size 思路 pri pan -c mat lis log

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2044

------------------------------------------------------------------------------------

題意:只能爬向+1或者+2的房間,求第a個房間到第b個房間的路線多少。

思路:遞歸,爬向第k個房間只能從第k-1或者k-2個房間爬動,所以相加遞歸即可。

註意:

遞歸的題目一般要用數組存上所求的解,防止給出較大值時造成的TLE。

代碼:

技術分享圖片
#include<cstdio>
#include<cstring>
#include
<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> using namespace
std; typedef long long ll; const double PI = acos(-1.0); const double eps = 1e-6; ll ans[55][55]={0}; ll f(int a,int b){ if(b-a==1) return 1; if(b-a==2) return 2; else return ans[a][b-1]+ans[a][b-2]; } int main(void){ for(int i=1;i<55;i++){ for(int j=i+1;j<55;j++){ ans[i][j]
=f(i,j); } } int T=0; scanf("%d",&T); for(int i=0;i<T;i++){ int a,b; scanf("%d %d",&a,&b); printf("%lld\n",ans[a][b]); } return 0; }
View Code

HDU 2044(遞推&遞歸_A題)解題報告