一只小蜜蜂...
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 104512 Accepted Submission(s): 36946
Problem Description
有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行。请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数。
其中,蜂房的结构如下所示。
Input
输入数据的第一行是一个整数N,表示测试实例的个数,然后是N 行数据,每行包含两个整数a和b(0<a<b<50)。
Output
对于每个测试实例,请输出蜜蜂从蜂房a爬到蜂房b的可能路线数,每个实例的输出占一行。
Sample Input
2
1 2
3 6
Sample Output
1
3
题意:有一只小蜜蜂要从一个蜂房爬到另一个蜂房,只能爬向右侧相邻的蜂房,不能反向爬行,换句话说,假如蜜蜂在n处,那他只能爬向n+1和n+2,问你一共有多少种爬行路线。
题解:假设蜜蜂在n点,那么他只能由n-1或者n-2爬行过来,我们用dp[n]来表示从起点爬到某一点的路线总数,那么dp[n]=dp[n-1]+dp[n-2],那么只要蜂房a到b之间的蜂房个数一定,爬行路线就一定,即从a蜂房爬到b蜂房等价于从起点爬到b-a蜂房,所以a蜂房到b蜂房的路线总数为dp[b-a]。
#include<cstdio>
using namespace std;
typedef long long int ll;
const int maxn=50+7;
ll dp[maxn];
int a,b;
int bee()
{
dp[1]=1,dp[2]=2;
for(int i=3;i<maxn;i++)
dp[i]=dp[i-1]+dp[i-2];
return 0;
}
int main()
{
bee();
int T;
scanf("%d",&T);
while(T--){
scanf("%d %d",&a,&b);
printf("%lld\n",dp[b-a]);
}
return 0;
}