The partial sum problem
算法:搜索
描述 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的更多相关文章
- 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 ...
- NYOJ 927 The partial sum problem 【DFS】+【剪枝】
The partial sum problem 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 One day,Tom's girlfriend give him a ...
- NYoj The partial sum problem(简单深搜+优化)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=927 代码: #include <stdio.h> #include & ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Subset sum problem
https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...
- 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 ...
随机推荐
- 在同个工程中使用 Swift 和 Objective-C(Swift 2.0更新)-b
本节包含内容: Mix and Match 概述(Mix and Match Overview) 在同个应用的 target 中导入(Importing Code from Within the Sa ...
- 1、vs2012 mvc3项目ObjectContext类找不到的问题
在vs2012下找不到ObjectContext类,取而代之的是DBContext,实体对象的Attach方法,上下文的ObjectStateManager对象都找不到,解决办法: 在设计视图中打开E ...
- allegro 的光绘层概念
TOP层: board geometry/outline manufacturing/photoplot_outline etch/top ...
- poj2352 Stars
http://poj.org/problem?id=2352 #include <cstdio> #include <cstring> #define maxn 400000 ...
- POJ1860 Currency Exchange(最短路)
题目链接. 分析: 以前没做出来,今天看了一遍题竟然直接A了.出乎意料. 大意是这样,给定不同的金币的编号,以及他们之间的汇率.手续费,求有没有可能通过不断转换而盈利. 直接用Bellman-ford ...
- 【转】JAVA字符串格式化-String.format()的使用
原文网址:http://blog.csdn.net/lonely_fireworks/article/details/7962171 常规类型的格式化 String类的format()方法用于创建格式 ...
- 【转】MTK Android Driver知识大全
原文网址:http://www.cnblogs.com/biglucky/p/4413797.html 一.Display 1.lcm 相关概念1.1) MIPI接口:一共有三种接口:DBI(也做CP ...
- Silverlight获取DataGrid选中的行数据
注意触发的事件为:CurrentCellChanged后台代码: /// <summary> /// 获取datagrid当前选中的单元格数据 /// 如果绑定的数据源为实体的话默认选中的 ...
- Eclipse无法设置NDK路径的解决方法
如果在Eclipse中设置NDK路径的时候遇到下面的情况(尼玛,我的NDK设置呢?): 可以参考百度经验解决:http://jingyan.baidu.com/article/4d58d5413000 ...
- ubunt 基于deb 配置本地apt 源 分成仅本机使用,局域网使用2种
dpkg-scanpackages /software /dev/null | gzip>/software/Packages.gz