Best Practice 2755 magic pocket [Deep Search] or [motion gauge] or [common recursion] or [recurrence]

Source: Internet
Author: User

Total time limit:
10000 ms
Memory limit:
65536kb

There is a magic pocket, the total volume is 40, with which some items can be changed, the total volume of these items must be 40. John now has n items to get. The sizes of each item are A1, A2 ...... An. John can choose from these items. If the total size of the selected object is 40, John can use this magic pocket to get these items. The question is how many different ways John chooses an item.
Input
The first line of the input is a positive integer N (1 <= n <= 20), indicating the number of different items. In the next n rows, each row has a positive integer between 1 and 40, respectively given A1, A2 ...... An value.
Output
Number of methods for selecting different items.
Sample Input
3202020
Sample output
3

This question is classic and can be done in many ways. I have tried the following:

DFS: Time consumed 0 ms

#include <stdio.h>int arr[22], ans, n, sum;void DFS(int k){    if(sum >= 40){        if(sum == 40) ++ans;        return;    }    for(int i = k; i <= n; ++i){        sum += arr[i];        DFS(i + 1);        sum -= arr[i];    }}int main(){    int i;    scanf("%d", &n);    for(i = 1; i <= n; ++i)        scanf("%d", arr + i);    sum = ans = 0; DFS(1);    printf("%d\n", ans);    return 0;}

Normal recursion: 60 ms

#include <stdio.h>int arr[22], n;int getAns(int sum, int k){    if(sum == 0) return 1;    if(k == 0) return 0;    return getAns(sum, k - 1) + getAns(sum - arr[k], k - 1);}int main(){    int i;    scanf("%d", &n);    for(i = 1; i <= n; ++i)        scanf("%d", arr + i);    printf("%d\n", getAns(40, n));    return 0;}

DP: Time consumed 0 ms

# Include <stdio. h> int arr [22], n, DP [42] [22]; // DP [I] [J] indicates the number of bid I methods in the previous J items int main () {int I, j; scanf ("% d ", & N); for (I = 1; I <= N; ++ I) {scanf ("% d", arr + I ); DP [0] [I] = 1 ;}for (DP [0] [0] = I = 1; I <= 40; ++ I) {for (j = 1; j <= N; ++ J) {DP [I] [J] = DP [I] [J-1]; if (I-Arr [J]> = 0) DP [I] [J] + = DP [I-Arr [J] [J-1];} printf ("% d \ n", DP [40] [N]); Return 0 ;}

Recursive DP: Time consumed 0 ms

# Include <stdio. h> int N, sum [42]; // sum [I] indicates the number of methods that value can constitute I int main () {int I, j, temp; scanf ("% d", & N); for (I = 0, sum [0] = 1; I <n; ++ I) {scanf ("% d ", & temp); For (j = 40; j; -- j) {If (J + temp> 40) continue; If (sum [J]) sum [J + temp] + = sum [J]; }++ sum [temp];} printf ("% d \ n", sum [40]); return 0 ;}




Best Practice 2755 magic pocket [Deep Search] or [motion gauge] or [common recursion] or [recurrence]

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.