Title Description
Please write a function (allow the addition of sub-functions), calculate n x m of the checkerboard lattice (n is the horizontal lattice number, M is the vertical lattice number) along the respective edge line from the upper left to the lower right corner, the total number of ways to go, the request can not go back, that: can only go to the right and down, can not
Input Description:
Enter a two positive integer
Output Description:
return results
Input Example:
22
Output Example:
6
1 ImportJava.util.*;2 Public classMain {3 Public Static voidMain (string[] args) {4Scanner sc =NewScanner (system.in);5 while(Sc.hasnext ()) {6 intn =sc.nextint ();7 intm =sc.nextint ();8 System.out.println (Find (n, m));9 }Ten } One Public Static intFindintNintm) { A if(n = = 0 | | m = = 0) - return1; - Else the returnFind (N-1, m) + Find (n, m-1); - } -}
View Code
F (n,m) means the n*m matrix, the first step can go down, after the matrix into (n-1) *m, can also go to the right, the matrix becomes n (m-1), so f (n,m) =f (n-1,m) +f (n,m-1), obvious recursion. If the scale is large and the stack overflows, it can be converted into iterations. Do not know the analysis of the right, for reference only.
201301 JAVA Topic level 2-3