51Nod:1268 和为K的组合
第1行:2个数N, K, N为数组的长度, K为需要判断的和(2 <= N <= 20,1 <= K <= 10^9)
第2 - N + 1行:每行1个数,对应数组的元素A[i] (1 <= A[i] <= 10^6)
如果可以,输出:"Yes",否则输出"No"。
5 13
2
4
6
8
10
No

时间复杂度:O(2^n)
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6;
int a[maxn];
int n,k;
bool dfs(int i,int sum)
{
if(i==n) return sum==k;
if(dfs(i+1,sum)) return true;
if(dfs(i+1,sum+a[i])) return true;
return false;
}
int main()
{
cin>>n>>k;
for(int i=0;i<n;i++) cin>>a[i];
if(dfs(0,0)) cout<<"YES\n";
else cout<<"NO\n";
return 0;
}
51Nod:1268 和为K的组合的更多相关文章
- 51Nod 1268 和为K的组合
51Nod 1268 和为K的组合 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使 ...
- 51nod 1268 和为K的组合 dfs
题目: 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使他们的和为K.如果可以,输出:& ...
- 1268 和为K的组合 Meet in mid二分思路
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1268&judgeId=193772 给出n = 20个数,问其是 ...
- 51nod1268 和为K的组合(DFS)
1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 给出N个正整数组成的数组A,求能否从中选出若干个,使他们的和为K.如果可以 ...
- 经典K线组合图解 > 正文
日K线波段中上下影线的箱体操作法(完整) http://video.sina.com.cn/v/b/130809461-2486130757.html!!经典K线组合图解 > 正文 http:/ ...
- 51nod 1105(第K大数 二分套二分)
题目链接:http://www.51nod.com/onlineJudge/submitDetail.html#!judgeId=620811 参考自:https://blog.csdn.net/f_ ...
- 77. Combinations (java 求C(n,k)的组合,排除重复元素)
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 解析:同求全 ...
- 51Nod——T 1686 第K大区间
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 ...
- 51nod 1105:第K大的数
1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...
随机推荐
- js的单双引号
单引号开始: 有时候上边的不行 双引号开始. 一般最外边是单引号 属性是双引号. 如果属性中还是需要一个属性的话,那么我们用\“,里边用‘+data.id+'来区分. 今天又一次遇到一次单双引号,花了 ...
- rabbitmq 消息的状态转换
tutorial:http://www.rabbitmq.com/tutorials/tutorial-two-java.html 这里解释接收消息端关于 acknowledge和prefetch的设 ...
- 12月15日 session:Ruby on Rails Security Guide//从第3节开始没有学习//关于find_by 和where的区别用法思考。
http://guides.rubyonrails.org/security.html#user-management 2.session笔记见13日的随笔. http://www.cnblogs.c ...
- 4-1 contag_tag:返回HTMLtag.
jquery已经过时,做一遍,了解其他知识点. contag_tag(name, content_or_options_with_block = nil, options = nil, &bl ...
- php--------使用js生成二维码
php生成二维码有多种方式,可以在JS中,也可以使用php库,今天写的这个小案例是使用JS生成二维码. 其他方式可以看下一篇文章:php--------php库生成二维码和有logo的二维码 网站开发 ...
- js下载图片
DownloadImgZP = imgPath => { const image = new Image(); // 解决跨域 Canvas 污染问题 image.setAttribute('c ...
- thinkphp if标签
1.thinkphp框架中的if标签,用于html页面中.在html中编写php代码 1).从控制器中得到数据在循环中if else 判断:<volist name="system_r ...
- HDU-3506 Monkey Party (环形石子合并)
题目大意:n堆石子围成一圈,每堆石子的块数已知,每次可以将相邻的两堆合并到一堆,块数变为两堆之和,代价也为两堆石子块数之和.求合并到一堆的最小代价. 题目分析:先通过将前n-1依次个移到第n个后面,将 ...
- Oracle性能诊断艺术-学习笔记(索引访问方式)
环境准备: 1.0 测试表 CREATE TABLE t ( id NUMBER, d1 DATE, n1 NUMBER, n2 NUMBER, n3 NUMBER, n4 NUMBER, n5 NU ...
- Error when clicking other button after displaying Popup window(转)
原文地址:Error when clicking other button after displaying Popup window Hi, I'm developing a custom page ...