zoukankan      html  css  js  c++  java
  • 【递归】油桶问题

    【递归】油桶问题

    题目描述

    楚继光扬扬得意道:“当日华山论剑,先是他用黯然销魂掌破了我的七十二路空明拳,然后我改打降龙十八掌,却不防他伸开食指和中指,竟是六脉神剑,又胜我一筹。可见天下武学彼此克制,武学之道玄之又玄!……哎,谁用炒锅敲我头?”

    楚继光的老妈大声骂道:“玩个石头剪刀布都说得这般威风,炒菜没油了,快给我去装!”

    “这么凶干嘛?不就吹吹牛嘛。”楚继光边嘟嘟囔囔边走进储藏室,看到储藏室有N个油桶都装满了油,这N个油桶容积各不相同(容积为整数),楚继光需要M升油(M也为整数),请你不借助任何其他容器,判断能否直接在N桶油中取任意K桶(1≤K≤N)油,其油的总量正好是M升,如果可以,就输出yes,否则输出no。

    输入

    第一行为两个整数N,M,第二行为N个整数,即油桶的容积。

    输出

    输出结果即yes或者no。

    样例输入

    5 10
    1 2 3 1 1
    

    样例输出

    no
    分析:背包问题;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    const int maxn=1e4+10;
    const int dis[][2]={0,1,-1,0,0,-1,1,0};
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}
    int a[maxn]={1};
    int main()
    {
        int i,j,m,n,k,t;
        scanf("%d%d",&n,&m);
        rep(i,0,n-1)
        {
            scanf("%d",&k);
            if(k<=m)
            {
                for(j=m;j>=k;j--)a[j]|=a[j-k];
            }
        }
        printf("%s
    ",a[m]?"yes":"no");
        //system("pause");
        return 0;
    }
     
  • 相关阅读:
    匿名函数
    Python基础练习题5
    for循环实现一个注册小案例
    Python基础练习题4
    Python集合
    Python基础练习题3
    Python 元组和字典
    Python PEP8规范与python之禅
    Python基础练习题2
    常见的排序之冒泡排序
  • 原文地址:https://www.cnblogs.com/dyzll/p/5645478.html
Copyright © 2011-2022 走看看