数字组合

数字组合

链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1291


时间限制: 1000 ms         内存限制: 65536 KB

【题目描述】

有n个正整数,找出其中和为t(t也是正整数)的可能的组合方式。如:

n=5,5个数分别为1,2,3,4,5,t=5;

那么可能的组合有5=1+4和5=2+3和5=5三种组合方式。

 

【输入】

输入的第一行是两个正整数n和t,用空格隔开,其中1<=n<=20,表示正整数的个数,t为要求的和(1<=t<=1000);

接下来的一行是n个正整数,用空格隔开。

 

【输出】

和为t的不同的组合方式的数目。

【输入样例】

5 5
1 2 3 4 5

【输出样例】

3
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

int a[105],f[10005];
int main()
{
    int n,V;
    cin>>n>>V;
    for(int i=1;i<=n;i++)cin>>a[i];
    f[0]=1;
    for(int i=1;i<=n;i++)
        for(int j=V;j>=a[i];j--)
            f[j]+=f[j-a[i]];
    cout<<f[V]<<endl;
}


原文地址:https://www.cnblogs.com/EdSheeran/p/7631956.html