描述

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(≤N≤),represents the array contains N integers. The second line contains N integers,the ith integer represents A[i](-^≤A[i]≤^).The third line contains an integer K(-^≤K≤^).
输出
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!”.
样例输入

样例输出
Of course,I can!
Sorry,I can't!

两种方法:

第一种直接回溯dfs

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int n,m,flag;
int a[N];
int vis[N];
void dfs(int now,int num){
if(num>=m){
if(num==m){
flag=;
}
return;
}
for(int i=now;i<n;i++){
if(!vis[i]){
vis[i]=;
dfs(i+,num+a[i]);
if(flag){
return;
}
vis[i]=;
}
}
}
int main()
{
while(scanf("%d",&n)==){
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&m);
memset(vis,,sizeof(vis));
flag=;
dfs(,);
if(flag){
printf("Of course,I can!\n");
}else{
printf("Sorry,I can't!\n");
}
}
return ;
}

第二种类似01背包思想的dfs

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int n,m;
int a[N];
bool dfs(int cur,int num){
if(num>=m){
if(num==m){
return true;
}
return false;
}
if(cur>=n) return false;
if(dfs(cur+,num+a[cur])) return true;
return dfs(cur+,num); }
int main()
{
while(scanf("%d",&n)==){
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&m);
if(dfs(,)){
printf("Of course,I can!\n");
}else{
printf("Sorry,I can't!\n");
}
}
return ;
}

nyoj 927 The partial sum problem(dfs)的更多相关文章

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

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

  2. HDU 2058:The sum problem(数学)

    The sum problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. hdu 1016 Prime Ring Problem(dfs)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. 1192: 零起点学算法99——The sum problem(C)

    一.题目 http://acm.wust.edu.cn/problem.php?id=1192&soj=0 二.分析 要求从序列1,2,3,,,N,中截取一部分使他们的和为M 输入多组数据 输 ...

  5. LeetCode Path Sum II (DFS)

    题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...

  6. LeetCode Combination Sum II (DFS)

    题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...

  7. LeetCode Combination Sum III (DFS)

    题意: 在1-9这9个数字中选择k个出来,若他们的和为n,则加入答案序列,注意升序. 思路: 用DFS的方式,每次决定一个数字,共决策k次.假设上个决策是第i位为5,那么i+1位的范围就是6-9. c ...

  8. HDOJ-1016 Prime Ring Problem(DFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入n,代表有一个包含n个节点的环,在环中的节点中填入1,2...n-1,n,要求填入的数与左边的数 ...

  9. hdu 1016 Prime Ring Problem (dfs)

    一切见凝视. #include <cstdio> #include <iostream> #include <cstring> #include <algor ...

随机推荐

  1. bzoj1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区

    Description It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of wh ...

  2. 几个简单的css设置问题:div居中,ul li不换行 ,内容超出自动变省略号等

    1  div在页面居中的问题 1)position值为relative时(相对定位),css设置属性margin:0 auto;(0 auto,表示上下边界为0,左右则根据宽度自适应相同值,即居中)即 ...

  3. rabbitmq-c初探

    RabbitMQ着实是个好东西,当然了也有对C语言client开发的支持.例子和文档少的可怜,只能去项目里去查看example来理解,简单整理了一些,以免走些弯路.主要是在版本对应上,这点就没Mave ...

  4. telnet如何操作Memcached缓存系统?

    4.(1)telnet操作Memcached 许多语言都实现了连接memcached的客户端,其中以Perl.PHP为主.仅仅memcached网站上列出的语言就有:• Perl • PHP • Py ...

  5. VisualStudio.DTE 对象可以通过检索 GetService() 方法

    DTE dte = (DTE)GetService(typeof(DTE)); string solutionDir = System.IO.Path.GetDirectoryName(dte.Sol ...

  6. Android应用自动更新功能的实现!!!

    自动更新功能的实现原理,就是我们事先和后台协商好一个接口,我们在应用的主Activity里,去访问这个接口,如果需要更新,后台会返回一些数据(比如,提示语:最新版本的url等).然后我们给出提示框,用 ...

  7. C# 关于out关键字的用法(一个方法返回多个值的问题)

    通常一个方法只能返回一个值,但是如果在某些时候,我们想要返回多个值,例如某个方法将一个浮点数分割成一个整数和一个小数返回去.这个时候我们就要用到out关键字. 如果用ref也可以解决,但是用ref需要 ...

  8. phantomjs API

    phantomjs使用说明         phantomjs实现了一个无界面的webkit浏览器.虽然没有界面,但dom渲染.js运行.网络访问.canvas/svg绘制等功能都很完备,在页面抓取. ...

  9. Android 启动Service服务和发送Broadcast广播的常用方法

    一.先说Service服务. 1.利用setAction()方法来指定启动的Service服务 Intent intent = new Intent(); intent.setAction(" ...

  10. ios 文字图标

    如何使用自定义字体 在讲icon font之前,首先先来看看普通自定义字体是如何在ios中使用的,两个原理是一样的.这里以KaushanScript-Regular为例: Step 1: 导入字体文件 ...