The sixth question of Java Blue Bridge Cup 2014

Java Step 39 - Memory Search

Xiao Ming just watched the movie "The 39th Step". When he left the cinema, he counted the number of steps in front of the auditorium, and it
happened to be 39!

Standing in front of the steps, he suddenly thought of another question:

If I can only take 1 or 2 steps per step. Step left first, then alternate left and right, and the last step is to take the right foot, which
means that you have to take an even number of steps in total. So, how many different ways are there to climb the 39 steps?
Please take advantage of the computer to help Xiao Ming find the answer.

static int arr[][] = new int[40][2];
 
	public static void main(String[] args) {
    
    
       
    
		// TODO Auto-generated method stub
		long startTime = System.currentTimeMillis(); // 获取开始时间
		System.out.println(fun(39, 0));
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); // 输出程序运行时间
	}
 
	public static int fun(int n, int tep) {
    
    
       
    
		if (n < 0)
			return 0;
		if (arr[n][tep] != 0)
			return arr[n][tep];
		if (n == 0 && tep == 0) {
    
    
       
    
			arr[n][tep] = 1;
			return 1;
		}
		return arr[n][tep] = fun(n - 1, (tep + 1) % 2) + fun(n - 2, (tep + 1) % 2);
	}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325073534&siteId=291194637