码迷,mamicode.com
首页 > 其他好文 > 详细

Potato Sacks

时间:2018-12-30 17:20:58      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:分享图片   follow   line   return   sof   rmi   idea   font   fill   

 

Potato sacks come in different weight capacities (specified in pounds). Potatoes come in different weights. If you are given some number of potatoes of possibly different weights (specified in pounds), determine if it is possible to exactly fill a potato sack of a given capacity using some or all of the potatoes.

技术分享图片

Input

The first line of input contains a single decimal integer P, (1P100), which is the number of data sets that follow. Each data set should be processed identically and independently.

Each data set consists of a single line of input containing 12 space separated positive integers. They are the data set number, K, followed by the capacity, CC, of the potato sack in pounds, (10C30),

followed by the weights of 10 potatoes in pounds. A potato will not weigh more than 3 pounds.

Output

For each data set there is a single line of output.

The output line consists of the data set number, K, followed by a single space, the word "YES" if the potato sack can be filled exactly to capacity C pounds or the word "NO" if it cannot be filled exactly.

样例输出

 2

1 20 3 2 1 3 3 2 3 2 1 1
2 25 3 3 3 3 3 3 3 3 3 3

样例输出

 1 YES

2 NO



idea: 简单来说,判断能否从这么多数中找能组成一个数

技术分享图片
#include <iostream>
#include <cstring>
using namespace std;
int n, ans, k;
int a[12];
bool dfs(int i, int sum)
{
    if(i==10)
        return sum==ans;
    if(dfs(i+1, sum))
        return true;
    if(dfs(i+1, sum+a[i]))
        return true;
    return false;
}
int main()
{
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        cin >> k >> ans;
        for(int j=0;j<10;j++)
        {
            cin >> a[j];
        }
        if(dfs(0, 0))
            printf("%d YES\n",k);
        else
            printf("%d NO\n",k);
    }
}
View Code

source:  2018 ICPC Greater New York Regional Contest

 

Potato Sacks

标签:分享图片   follow   line   return   sof   rmi   idea   font   fill   

原文地址:https://www.cnblogs.com/jmzIT/p/10199779.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!