洛谷P2437 Java

题目出处点这里
在这里插入图片描述思路:由于是m到n的走法,因此第一想法是开N*N的二维数组,先把6之前的走法填到里面去,如图:
在这里插入图片描述左下部分划掉是因为m<n,容易发现从m到n的走法实际上就是数列{1,2,3,5,8,……}的第n-m项,又因为此数列是递推数列,因此不难得出代码如下:

package reintroduction_recursion;

import java.math.BigInteger;
import java.util.Scanner;

public class P2437 {

	static BigInteger first = new BigInteger("1");
	static BigInteger two = new BigInteger("2");
	static BigInteger res = new BigInteger("0");

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		int n = sc.nextInt();
		if (n - m >= 3) {
			for (int i = 3; i <= n - m; i++) {
				res = first.add(two);
				first = two;
				two = res;
			}
		}
		System.out.println(res);
	}
}

猜你喜欢

转载自blog.csdn.net/TXXERIN/article/details/107222779