Algorithms: m identical apples are placed on n identical dishes. How many results are there?

Source: Internet
Author: User

This is also a question asked by netizens when answering questions. m of the same apple is put on n identical dishes, and the arrangement and combination method is not easy to deal with because of the same. There are many ideas for solving this problem. Here is a better understanding of the solution: ① At least one plate is put, so that at least one plate, n first put n dishes, the remaining m-n can be randomly placed. ② the minimum number of dishes is not put. In this way, the remaining n-1 dishes can still be implemented using m code:

Import java. util. role; public class MNAppleTest {public static void main (String [] args) {role in = new role (System. in); int N, M; // M Apple, N Disk M = in. nextInt (); N = in. nextInt (); System. out. print ("Total" + cal (M, N) + "result");} private static int cal (int m, int n) {if (m <0) return 0; if (m = 0 | n = 1) return 1; return cal (m-n, n) + cal (m, n-1 );}}

 

Enter 10, 20, and press ENTER 1020. There are 42 results

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.