一只小蜜蜂... HDU - 2044

有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行。请编程计算蜜蜂从蜂房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
  • 从1-2有1种方法,从1-3可以由1-3或1-2-3,总共2种,由1-4可由1-2-3-4或1-3-4或1-2-4总共3种,可以这样想:

    想到达4必需到达3或2,然后计算到达3或2的所有路线,

  • 加起来就是所有的路线数,f(4)=f(3)+f(2);

  • 由此可以想到斐波那契数列

  • #include <iostream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    typedef long long ll;
    ll dp[61];
    int main()
    {
        dp[1]=1;
        dp[2]=2;
        for(int i=3; i<=60; i++)
        {
            dp[i]=dp[i-1]+dp[i-2];
        }
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n,m;
            scanf("%d%d",&n,&m);
            printf("%lld\n",dp[m-n]);
        }
        return 0;
    }
    

猜你喜欢

转载自blog.csdn.net/chen_zan_yu_/article/details/84449389
今日推荐