算法:搜索

描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choose some integers from the N integers and the sum of them is equal to K.


输入There are multiple test cases.

Each test case contains three lines.The first line is an integer N(1≤N≤20),represents the array contains N integers. The second line contains N integers,the ith integer represents A[i](-10^8≤A[i]≤10^8).The third line contains an integer K(-10^8≤K≤10^8).输出If
Tom can choose some integers from the array and their them is K,printf ”Of course,I can!”; other printf ”Sorry,I can’t!”.样例输入4

1 2 4 7

13

4

1 2 4 7

15

样例输出

Of course,I can!

Sorry,I can't!

代码:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
using namespace std;
int n,m,a[25],flag;
int dfs(int i,int sum)
{
if(i==n)
return sum==m;
if(dfs(i+1,sum))
return 1;
if(dfs(i+1,sum+a[i]))
return 1;
return 0; }
int main()
{
int i,j,k;
while(cin>>n)
{
for(i=0;i<n;i++)
cin>>a[i];
cin>>m;
if(dfs(0,0)) cout<<"Of course,I can!"<<endl;
else cout<<"Sorry,I can't!"<<endl;
}
return 0;
}

The partial sum problem的更多相关文章

  1. NYOJ--927--dfs--The partial sum problem

    /* Name: NYOJ--927--The partial sum problem Author: shen_渊 Date: 15/04/17 19:41 Description: DFS,和 N ...

  2. NYOJ 927 The partial sum problem 【DFS】+【剪枝】

    The partial sum problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 One day,Tom's girlfriend give him a ...

  3. NYoj The partial sum problem(简单深搜+优化)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=927 代码: #include <stdio.h> #include & ...

  4. ACM题目————The partial sum problem

    描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choo ...

  5. nyoj 927 The partial sum problem(dfs)

    描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choo ...

  6. 2017-5-14 湘潭市赛 Partial Sum 给n个数,每次操作选择一个L,一个R,表示区间左右端点,该操作产生的贡献为[L+1,R]的和的绝对值-C。 0<=L<R<=n; 如果选过L,R这两个位置,那么以后选择的L,R都不可以再选择这两个位置。最多操作m次,求可以获得的 最大贡献和。

    Partial Sum Accepted : Submit : Time Limit : MS Memory Limit : KB Partial Sum Bobo has a integer seq ...

  7. summary of k Sum problem and solutions in leetcode

    I found summary of k Sum problem and solutions in leetcode on the Internet. http://www.sigmainfy.com ...

  8. Subset sum problem

    https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...

  9. HDu 1001 Sum Problem 分类: ACM 2015-06-19 23:38 12人阅读 评论(0) 收藏

    Sum Problem Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

随机推荐

  1. 百度地图LBS云平台读写数据操作类

    最近写了个叫<行踪记录仪>的手机软件,用了百度云来记录每个用户的最近位置,以便各用户能在地图上找到附近的人,为此写了个类来读写数据,大致如下: import java.util.Array ...

  2. MYSQL主从同步测试

    参考: http://www.cnblogs.com/zgx/archive/2011/09/13/2174823.html 注意选建同步用户,其它的都按步就搬. 还有,不要让IPTABLES坏事,开 ...

  3. COJ 0650 绝世难题(一) 可爱的仙人掌

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=620 绝世难题(一) 可爱的仙人掌 难度级别:E: 运行时间限制:1000m ...

  4. 【递归】Vijos P1132 求二叉树的先序序列(NOIP2001普及组第三题)

    题目链接: https://vijos.org/p/1132 题目大意: 给定二叉树的中序和后序遍历,求该二叉树先序遍历. 题目思路: [递归] 这题妥妥递归. 二叉树先序根左右,中序左根右,后序左右 ...

  5. Hyper-V连接虚拟机异常,“无法进行连接,因为可能无法将凭据发送到远程计算机”

    前两天把公司的TFS从2010升级到TFS2012(昨天又升级到TFS2013).今天使用服务器Hyper-V连接虚拟机时居然报错了. 翻看各种日志,虽然错误大一对一对地,但找不到任何有用的信息.无奈 ...

  6. 两个bootstrap插件bootstrap-select和bootstrap-paginator

    基于bootstrap的选择器 http://silviomoreto.github.io/bootstrap-select/ <label for="androids" c ...

  7. Android Studio SDK更新失败解决方法

    1.设置host 首先在windows/system32/drivers/etc/hosts中设置hosts,需要管理员权限.对hosts进行编辑: sudo vim hosts #Google主页 ...

  8. Android Studio 遇见的第一个Error

    最近在国内多次尝试在Eclipse下更新SDK无果后,最后终于通过FQ后结束了Google服务器无法访问的噩梦. 顺着墙外的梯子,一并下载Google的Android Studio尝鲜,安装成功后,就 ...

  9. java集合简介

    java集合主要包括以下几点 Java 集合概述 Collection 接口 Iterator 接口 Set List Map Collections 工具类 Enumeration 1.java集合 ...

  10. java中的Package语句和import语句

    在实际项目中会有成百上千个类,我们把近似的类放在同一个包里面,比如把实体类放在实体类包里面   package 为解决类的 命名冲突问题而引入的机制. package语句作为Java源文件的第一条语句 ...