算法:搜索

描述 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. FileUpload 改变控件显示的文字

    浏览

  2. [Android] 输入系统(一)

    Android输入系统是人与机器交互最主要的手段.我们通过按键或者触碰屏幕,会先经由linux产生中断,进行统一的处理过后,转换成Android能识别的事件信息,然后Android的输入系统去获取事件 ...

  3. iso18092-2004中ISO14443,Felica的关系

    ISO18092协议介绍了P2P通讯中的ACTIVE模式和PASSIVE通讯模式,其实ISO18092使用了ISO14443协议和非国际标准的FELICA通讯协议,这里总结了一下.

  4. INKDIE

    ink die是指分割出来的芯片未能达到原装芯片要求的那些U盘芯片,也就是俗称的黑片. 一整块圆硅分割制成芯片之后需要对其质量进行筛选,根据筛选的要求不同分为三个等级:原装的(Original).白片 ...

  5. listView中setOnItemClickListener和getSelectedItemPosition()取不到position问题

    //也可以采用通过listview 索引 取得 item 可以转化到 cursor ,pos 是在listview 的选中事件中赋值. //Cursor v2 = (Cursor) listView. ...

  6. Linux环境下使用图形化界面的SVN客户端软件-RabbitVCS

    如果想在Linux环境下使用图形化界面的SVN客户端软件,那么RabbitVCS绝对是首选,可以媲美Windows环境下用的TortoiseSVN,甚至连操作都基本一样,所以强烈推荐给各位童鞋. Ra ...

  7. Java学习日记-2.4 浮点数

    http://wenku.baidu.com/view/e68b2ed5312b3169a451a4e2.html

  8. D - 棋盘游戏 - HDU 1281(二分图匹配)

    分析:先求出来最大匹配数,然后用匹配的点一个一个去除看看能否达到最大匹配,能的话就是关键点(很暴力啊),不过竟然才31ms ************************************** ...

  9. 同时安装Xcode6和Xcode7导致出现N多UUID 模拟器解决办法

    [摘要:1.完整退出Xcode 和 摹拟器 2.末端中输进以下两居指令 $ sudo killall -9 com.apple.CoreSimulator.CoreSimulatorService$ ...

  10. Windows 已在 DImageProcess.exe 中触发一个断点。

    其原因可能是堆被损坏,这说明 DImageProcess.exe 中或它所加载的任何 DLL 中有 Bug. 原因也可能是用户在 DImageProcess.exe 具有焦点时按下了 F12. 输出窗 ...