得到整数X

题目
问题描述
某君有 n个互不相同的正整数,现在他要从这 n 个正整数之中无重复地选取任意个数,并仅通过加法凑出整数 XXX。求某君有多少种不同的方案来凑出整数 XXX。

输入格式
第一行,输入两个整数 n,X(1≤n≤20,1≤X≤2000),X(1≤n≤20,1≤X≤2000)。

接下来输入 n 个整数,每个整数不超过 100。

输出格式
输出一个整数,表示能凑出 XXX 的方案数。

样例输入
6 6

1 2 3 4 5 6

样例输出
4


 #include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
//    freopen("1.txt","r",stdin);
//    freopen("2.txt","w",stdout);
    int n,m,c,count=0,sum=0;
    int a[20];
    cin>>n>>m;
    int i,j;
    for(i=0;i<20;i++)
        cin>>a[i];
    int ans=0;
    for(i=0;i<(1<<n);i++)
    {
        int num=0;
        for(j=0;j<n;j++)
        {
            if(i&(1<<j))
            {
                num+=a[j];
            }
        }
        if(num==m)
        {
            ans++;
        }
    }
    cout<<ans;
    return 0}

猜你喜欢

转载自blog.csdn.net/qq_42403069/article/details/86524379