Acwing-278-数字组合(背包)

链接:

https://www.acwing.com/problem/content/280/

题意:

给定N个正整数A1,A2,…,AN,从中选出若干个数,使它们的和为M,求有多少种选择方案。

思路:

背包.

代码:

#include <bits/stdc++.h>
using namespace std;

int a[110], Dp[10010];
int n, m;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1;i <= n;i++)
        scanf("%d", &a[i]);
    Dp[0] = 1;
    for (int i = 1;i <= n;i++)
    {
        for (int j = m;j >= a[i];j--)
            Dp[j] += Dp[j-a[i]];
    }
    printf("%d\n", Dp[m]);

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/11494503.html