The Apple of dynamic planning

Source: Internet
Author: User

title Description: Put m apples on n plates, can have plates empty, but no order, for example, 5 apples to 3 plates, 1+1+3 and 1+3+1 as the same method, the total number of methods.
Dynamic Planning * * * *

ImportJava.util.Scanner;2 Public classmain{3 Public Static voidMain (string[] args)4 {5 /*6 * with dp[i][j] means the number of ways I apples are placed on the J plate7 *i an Apple into a J-plate (no order) in two cases8 * * There is a tray empty (this situation contains multiple plates for empty condition)9 * method number is dp[i][j-1]Ten all dishes are not empty, then each plate has at least one apple, the remaining One *i-j apples are placed on a J plate, the number of methods is dp[i-j][j] A * then dp[i][j] = Dp[i][j-1]+dp[i-j][j] - (also consider when J>i, Dp[i][j]=dp[i][i]) - */ theScanner sc =NewScanner (system.in); - while(Sc.hasnext ()) - { - intM =sc.nextint (); + intN =sc.nextint (); - int[] DP =New int[M+1] [N+1]; + for(inti=1;i<=n;i++) A { atDp[1][i]=1; -Dp[0][i]=1; - } - for(inti=1;i<=m;i++) - { -Dp[i][1]=1; inDp[i][0]=0; - } to for(inti=2;i<=m;i++) + { - for(intj=2;j<=n;j++) the { * if(i>=j) $DP[I][J] = dp[i][j-1]+dp[i-J] [j];Panax Notoginseng Else -dp[i][j]=Dp[i][i]; the } + } A System.out.println (Dp[m][n]); the } + } -}

The Apple of dynamic planning

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.