Problem of putting apples

Source: Internet
Author: User

1. Problem of putting apples

Problem Description:

Put m the same apples on n the same plate, allow some plates to be empty, ask how many different ways of putting? (denoted by k) Note: 5,1,1 and 1,5,1 are the same kind of sub-method.

Input data:

The first line is the number of test data T (0<=T<=20), and each of the following lines contains two integers m and n, separated by a space. 1<=m,n<=10.

Output requirements:

For each set of data entered M and N, the corresponding k is output in one line.

Input Sample:

1

9}

Sample output: 8

2. Code implementation
1  PackageCom.wcy.october;2 3 ImportJava.util.Scanner;4 5 /**6 * Date: October 23, 20167 * Problem Description: Put m same apple on n same plate, allow some plates to be empty, ask how many different ways are there? (denoted by k) Note: 5,1,1 and 1,5,1 are the same kind of sub-method. 8 * Input data: The first line is the number of test data T (0<=T<=20), each of the following lines contains two integers m and n, separated by a space. 1<=m,n<=10. 9 * Output requirements: For each set of input data m and N, a row to output the corresponding K. Ten * Input Sample One * 1 A * 7 3 - * Output Example - * 8 the  *  - * Method Analysis: - * 1. All different placement methods can be divided into two categories: at least one plate is empty and all the plates are empty. We can calculate the number of these two kinds of placement methods separately, and then add them together. For at least one empty - * on the plate, the number of M apples placed on n plates is the same as the number of N-1 plates placed with M apples. For all the dishes are not empty, then n plates placed m apple placement method + * The number is equal to the number of places where n plates are placed m-n apples. We can solve this problem by recursive method.  - * 2, set F (m,n) for M apples, n plates of the number of methods, then the first discussion of N, if n>m, there must be n-m a plate is always empty, remove them to put the number of apples placed no influence; + * That is, if (n>m) f (m,n) =f{m,m}. When N<=m, different methods can be divided into two categories: that is, there is at least one plate empty or all the dishes have apples, the former is the equivalent of A * F (m,n) =f (m,n-1); In the latter case, an apple can be removed from each plate without affecting the number of different methods of discharge, i.e. F (m,n) =f (m-n,n). The total number of put apples is equal to the two at * and, i.e. F (m,n) =f (m,n-1) + f (m-n,n). The entire recursive process is described as follows: - * int f (int m,int n) { - * if (n==1| | m==0) return 1; - * if (n>m) return F (m,m); - * return F (m,n-1) +f (m-n,n)} - * 3, export conditions Note: When the n=1 is, all apples must be placed on a plate, all return 1, when there is no apple can be placed, defined as 1 kinds of release method. The two paths of recursion, the first n will gradually decrease, will eventually reach in * Export N==1, the second m will gradually decrease, because when n>m, we will return F (m,m) so will eventually reach the exit m==0. -  */ to  Public classApple { +  -     /** the * Find the number of placement methods *      * @paramm number of apples $      * @paramn Number of platesPanax Notoginseng      * @returnnumber of placement methods -      */ the      Public intGetResult (intMintN) { +         if(n = = 1 | | m = = 0) { A             return1; the}Else if(M <N) { +             returnGetResult (M, m); -}Else { $             returnGetResult (M., n-1) + GetResult (M-n, N); $         } -     } -      the     /** - * User page TestWuyi      * @paramargs the      */ -@SuppressWarnings ("Resource") Wu      Public Static voidMain (string[] args) { -Apple Apple =NewApple (); AboutScanner reader =NewScanner (system.in); $         intNumber =reader.nextint (); -         int[] result =New int[number]; -         intm = 0;//Number of apples -         intn = 0;//Number of plates A          for(inti = 0; I < number; i++) { +m =reader.nextint (); then =reader.nextint (); -Result[i] =Apple.getresult (M, n); $         } the          the          for(inti = 0; i < result.length; i++) { the System.out.println (Result[i]); the         } -     } in}

Problem of putting apples

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.