
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]);
}
}
}