HDU 2044 一只小蜜蜂... (递推)

xiaoxiao2021-02-28  141

题目

Problem Description

有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行。请编程计算蜜蜂从蜂房 a 爬到蜂房 b 的可能路线数。 其中,蜂房的结构如下所示。

Input

输入数据的第一行是一个整数 N ,表示测试实例的个数,然后是 N 行数据,每行包含两个整数 a b (0<a<b<50) 爬到蜂房 b 的可能路线数,每个实例的输出占一行。

Sample Input

2 1 2 3 6

Sample Output

1 3

分析

经过推导,得到从蜂房 a 爬到蜂房 b 的路径个数为从蜂房 a 爬到蜂房 b1 的路径个数加上从蜂房 a 爬到蜂房 b2 的路径个数,因此此题是一个递推题。可以建立一个数组 ans ans[i]=ans[i1]+ans[i2] ,当前的 ans[i] 就是 ans[ba] 。不要用递归,这样会导致超时,因为题目的数据较小,可以先打表,然后输出对应的结果。


代码

#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<queue> #include<stack> #include<vector> #include<cmath> #include<set> #include<map> #include<cstdlib> #include<functional> #include<climits> #include<cctype> #include<iomanip> using namespace std; typedef long long ll; #define INF 0x3f3f3f3f #define mod 1e9+7 #define clr(a,x) memset(a,x,sizeof(a)) const double eps = 1e-6; ll d[1000]; int main() { int t; d[1]=1; d[2]=2; for(int i=3;i<50;i++) { d[i]=d[i-1]+d[i-2]; } cin>>t; int a,b; while(t--) { cin>>a>>b; cout<<d[b-a]<<endl; } return 0; }
转载请注明原文地址: https://www.6miu.com/read-37573.html

最新回复(0)