Gym-100712G-Heavy Coins

简单DFS

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int n,s,ans,a[15];

void dfs(int loc,int sum,int num)
{
    if(sum>=s)
    {
        ans=max(ans,num);
        return ;
    }
    if(loc<0)
        return ;
    dfs(loc-1,sum,num);
    dfs(loc-1,sum+a[loc],num+1);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ans=-1;
        scanf("%d%d",&n,&s);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        dfs(n-1,0,0);
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(Gym-100712G-Heavy Coins)