M apples put on n plates problem

Source: Internet
Author: User

Question One

Problem Description: Put m the same apples on n the same plate, allow some tray empty, ask how many different methods? (Note: 5,1,1 and 1,1,5 are the same kind of sub-method)

Problem Solving Analysis:

Set F (m,n) for M apples, N for the number of plates, then the n is discussed first,

When n>m: There must be n-m a plate is always empty, remove them on the number of Apple methods to be placed no effect. i.e. if (n>m) f (m,n) = f (m,m)

When n <= m: Different methods can be divided into two categories: 0 of the program number, not including 0 of the program number

1, contains 0 of the program number, that is, at least one plate empty, that is, the equivalent of F (m,n) =f (m,n-1);

2, does not contain 0 of the program number, that is, all the plates have apples, equivalent to take an apple from each plate, does not affect the number of different methods, namely F (m,n) =f (m-n,n). The total number of put apples is equal to the sum of the two, that is F (m,n) =f (m,n-1) +f (m-n,n )

Description of the recursive export conditions:

When n=1, all apples must be placed on a plate, so return 1;

When the m==0 (no Apple can be placed), defined as 1 kinds of release method;

Recursive

int fun (int m, int n)//m apples on n plates There are several methods {if (m==0 | | n==1) return 1;    if (n>m) return fun (m,m); else return Fun (m,n-1) +fun (m-n,n);}

Dynamic planning

Put the Apple Int main () {    int apple, plate;    if (apple  < 0 | |  apple > 10 | |  plate < 1 | | &NBSP;PLATE&NBSP;&GT;&NBSP;10)     {        cout  << -1 << endl;        return -1;     }    vector<vector<int> > ivec (11,  Vector<int> (11,0));     for (int i=0; i < 11; i++)      {        ivec[0][i] = 1;         ivec[i][1] = 1;    }    for ( Int i = 1; i <= 10; ++i)     {         for (int j&NBSP;=&NBSP;1;&NBSP;J&NBSP;&LT;=&NBSP;10;&NBSP;++J)         {             if (j <= i)                  ivec[i][j] = ivec[i][ J-1] + ivec[i-j][j];            else &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IVEC[I][J]  = ivec[i][j];        }    }     cout << ivec[apple][plate] << endl;    return  0;}

Question Two

Problem Description: The sum of integer n divided into k integers and each number greater than or equal to a less than or equal to B, the number of different methods

int Dynamics (int n, int k, int min)//n is divided into k integers, the smallest is greater than or equal to Min, the largest is not more than b{if (n < min) return 0;    When the remainder is smaller than min, it does not meet the requirements, return 0 if (k = = 1) return 1;    int sum = 0;    for (int t = min; t <= B; t++) {sum + = Dynamics (n-t, k-1, T); } return sum;}

Question Three

M----> Same, n----> Same, cannot be empty

How many ways to put m apples into n plates? Note, for example, that the two scenarios, such as 1, 2, and 2 and 1, are a scenario.

Idea, first put each plate of an apple, so the problem is converted to: M-n an apple into the N plate, the plate allows empty, that is, problem 1

M apples put on n plates problem

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.