一只小蜜蜂…
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 103323 Accepted Submission(s): 36538
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
根据题意可以看出可能路线数与a和b间的距离有关,直接创建一个数组dp[n]来表示当a和b距离为n时有多少种走法,因为只能往右走,比如说a,b,c紧密靠着(a < b < c),因为走到a和b以后再往右走一步就到c了,所以走到c的的走法就相当于走到a的走法 + 走到b的走法。
递推公式:f(n) = f(n-1) + f(n-2); (n > 2);
下面附上ac代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <queue>
#define INF 0x7ffffff
using namespace std;
typedef long long int ll;
ll dp[60];
int main() {
dp[1] = 1;
dp[2] = 2;
dp[3] = 3;
for(int i = 4;i < 55;i++){
dp[i] = dp[i-1] + dp[i-2];
}
int n;
cin >> n;
while(n--){
int a,b;
cin >> a >> b;
cout << dp[b-a] << endl;
}
return 0;
}