其中,蜂房的结构如下所示。
Input
输入数据的第一行是一个整数N,表示测试实例的个数,然后是N 行数据,每行包含两个整数a和b(0<a<b<50)。
Output
对于每个测试实例,请输出蜜蜂从蜂房a爬到蜂房b的可能路线数,每个实例的输出占一行。
Sample Input
2
1 2
3 6
Sample Output
1
3
这道水题不用DP,用菲波那切数列可以简单的解决,但是要注意的是,菲波那切数列在40位以后有可能超过32位,所以数组用单纯的int型不能正确存储,得用long long型或
—int64型存储。
我的代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<string.h>
using namespace std;
int main()
{
int n, e1, e2;
long long r[100];//注意用的是long long型
scanf("%d", &n);
while(n--)
{
int i;
cin >> e1 >> e2;
if (e1 > e2)
swap (e1, e2);
e1 = e2 - e1 ;
r[0] = 1; r[1] = 1;
for (i = 2; i <= 51; i++)
r[i] = r[i-1] + r[i-2];
cout << r[e1] << endl;
}
return 0;
}