201301 JAVA Topic level 2-3

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.