算法:搜索

描述 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. SPSS方差分析

    1.overall:一切的,全面地 单因素方差分析:分析--比较均值--单因素ANOVA.多因素方差分析:分析--一般线性模型--单变量. 单因素方差分析和单变量方差分析区别:单因素针对的是自变量(自 ...

  2. Macro Substitution

    看<C程序设计语言>(英文版)学到的两个用法. 两个很简单的宏用法. #的用法: if, however, a parameter name is preceded by a # in t ...

  3. 【转】(DT系列三)系统启动时, dts 是怎么被加载的

    原文网址:http://www.cnblogs.com/biglucky/p/4057481.html 一,主要问题:系统在启动的时候,是怎么加载 dts的:Lk,kernel中都应调查. 二:参考文 ...

  4. Android SDK的下载和安装

    Android SDK包含的各种库文件.文档.源代码.示例代码……都是通过SDK Tools来下载和安装的,所以我们需要首先下载和安装SDK工具包(SDK Tools Package). 这一步我们可 ...

  5. PermGen space 与 Java heap space

    1.java.lang.OutOfMemoryError: PermGen spacePermGen space的全称是Permanent Generation space,是指内存的永久保存区域Ou ...

  6. Android开发学习之Camera

    今天本来想写一篇关于百度地图定位SDK的文章的,无奈根据官网提供的例子编写的程序始终无法运行,所以这个计划只能落空.那么今天要与大家分享的是Camera,即照相机.随着硬件能力的大幅提升,手机上各种依 ...

  7. YKCW6-BPFPF-BT8C9-7DCTH-QXGWCYQ7PR-QTHDM-HCBCV-9GKGG-TB2TM

    YKCW6-BPFPF-BT8C9-7DCTH-QXGWCYQ7PR-QTHDM-HCBCV-9GKGG-TB2TM

  8. SKView类

    继承自 UIView:UIResponder:NSObject 符合 NSCoding(UIView)UIAppearance(UIView)UIAppearanceContainer(UIView) ...

  9. Mysql + keepalived 实现双主热备读写分离【转】

    Mysql + keepalived 实现双主热备读写分离 2013年6月16日frankwong发表评论阅读评论   架构图 系统:CentOS6.4_X86_64软件版本:Mysql-5.6.12 ...

  10. android不自动弹出虚拟键盘

    如果是Activity的话 在 Manifest.xml 相应的 Activity 里添加 android:windowSoftInputMode="adjustPan|stateHidde ...