Catch fish in C Language

Source: Internet
Author: User

Catch fish in C Language

Fish fishing problem: 20 buckets with 10 fish in each bucket. The number of fish captured in each bucket is random. Each bucket can only be caught once, ask how many sort of 180 entries are caught.

Analysis: Let's look at the dual problem of this problem. After capturing 180 fish, 20 fish are left in 20 buckets, different capture methods correspond to the distribution of these fish in 20 buckets, as a result, the problem is transformed into how many different classification methods are used to divide 20 fish into 20 buckets (this problem is also equivalent to how many different methods are used to divide 180 fish into 20 buckets).

Dp [I] [j]: The method for placing j fish in the first I bucket is to place j-k in the first I-1 bucket (0 <= k <= 10) (Because bucket I can only hold 0 to 10 fish) the total number of fish methods. We can obtain the state equation: f (I, j) = sum {f (I-1, j-k), 0 <= k <= 10}

 

/* Fish fishing: put 20 fish in 20 buckets, and put up to 10 fish in each bucket. Find all the arrangement methods/* bottom-up DP f (I, j) = sum {f (I-1, j-k), 0 <= k <= 10}/* This method tests 20 barrels of 180 fish, comparison with recursive speed */void CatchFish () {int dp [21] [200]; // number of methods for placing j fish in the first I bucket int bucketN = 20; int fishN = 20; memset (dp, 0, sizeof (dp); for (int I = 0; I <= 10; ++ I) // initialize valid status {dp [1] [I] = 1 ;}for (int I = 2; I <= bucketN; ++ I) // start from the second bucket {for (int j = 0; j <= fishN; ++ j) {for (int k = 0; k <= 10 & j-k> = 0; ++ k) {dp [I] [j] + = dp [I-1] [j-k] ;}} printf (% d, dp [bucketN] [fishN]);}

 

Related Article

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.