
2 1 2 3 6
1 3
1、“蜜蜂只能爬向右侧相邻的蜂房”准确来说,包含三个方向:正右方,右下方,右上方。
2、到 1 只有一条路(本身嘛),到 2 有一条路线(1 —> 2),到 3 有两条路线(1 —>3 或者 2 —> 3),到 4 有 3 条路线(到 2 的 1 条加上到 3 的 2 条),到 5 有 5 条路线(到 3 的 2 条加上到 4 的 3 条)……
3、以此类推,从原点到达各个蜂房的路线数分别为:
| 蜂房号 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | …… |
| 路线数 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | …… |
不难看出,这是一个斐波那契数列!只要能看出这个关键点,AC这道题就很容易了。
import java.util.Scanner;
public class Main {
static long[] count = new long[50];
static {
count[0] = count[1] = 1;
for (int i = 2; i < 50; i++) {
count[i] = count[i - 1] + count[i - 2];
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
while (n-- != 0) {
int a = scanner.nextInt();
int b = scanner.nextInt();
int num = b - a;
System.out.println(count[num]);
}
}
}原文地址:http://blog.csdn.net/u011506951/article/details/24734445