The partial sum problem

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描写叙述
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 <stdio.h>
#include <stdlib.h>
int n, arr[22], sum, vis[22], ok, count;
const char *sam[] = {"Sorry,I can't!\n", "Of course,I can!\n"}; int cmp(const void *a, const void *b){
return *(int *)a - *(int *)b;
} void DFS(int k){
if(count == sum){
ok = 1; return;
} for(int i = k; i < n; ++i){
if(i && arr[i] == arr[i-1] && !vis[i-1]) //cut
continue;
if(count > sum && arr[i] > 0) return; //cut count += arr[i]; vis[i] = 1;
DFS(i + 1);
if(ok) return;
count -= arr[i]; vis[i] = 0;
}
} int main(){
while(scanf("%d", &n) == 1){
for(int i = 0; i < n; ++i){
scanf("%d", arr + i);
vis[i] = 0;
}
scanf("%d", &sum);
qsort(arr, n, sizeof(int), cmp);
count = ok = 0; DFS(0);
printf(ok ? sam[1] : sam[0]);
}
return 0;
}

NYOJ 927 The partial sum problem 【DFS】+【剪枝】的更多相关文章

  1. 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 ...

  2. 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 ...

  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. The partial sum problem

    算法:搜索 描述 One day,Tom's girlfriend give him an array A which contains N integers and asked him:Can yo ...

  6. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  7. ACdream 1431——Sum vs Product——————【dfs+剪枝】

    Sum vs Product Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Others) S ...

  8. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  9. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

随机推荐

  1. 部分vivo和oppo手机,使用上传图片功能,可能会出现退出webview的现象(回退到app的入口页面)

    在公司的app里面嵌入了一个h5页面,h5页面有个使用图片上传功能,上传图片出现闪退的现象 问题描述:vivo手机,在app内的wap页面使用上传图片的功能,在选择好图片点击确认按钮后,出现退出整个w ...

  2. websocket调试工具

    http://www.blue-zero.com/WebSocket/ wss://yy.xxx.com/video/websocket/client.ws

  3. Haproxy实现web的页面的动静分离

    一.Haproxy概述: 概述:Haproxy是一个开源的高性能的反向代理或者说是负载均衡服务软件之一,由C语言编写而成,支持会话保持.七层处理.健康检查.故障修复后自动加载.动静分离.HAProxy ...

  4. [ZJOI2012]旅游(树的直径)

    [ZJOI2012]旅游 题目描述 到了难得的暑假,为了庆祝小白在数学考试中取得的优异成绩,小蓝决定带小白出去旅游~~ 经过一番抉择,两人决定将T国作为他们的目的地.T国的国土可以用一个凸N边形来表示 ...

  5. PKU 2288 Islands and Bridges 状态dp

    题意: 给你一张地图,上面有一些岛和桥.你要求出最大的三角哈密顿路径,以及他们的数量. 哈密顿路:一条经过所有岛的路径,每个岛只经过一次. 最大三角哈密顿路:满足价值最大的哈密顿路. 价值计算分为以下 ...

  6. IPMI远程管理一点记录

    http://www.07net01.com/storage_networking/IPMIyuanchengguanliyidianjilu_53093_1357975254.html

  7. Cocos2d-x 3.0 Schedule in Node

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  8. Mac系统下安装ant

    看了一些别人怎么在mac下安装ant,大体都是从官网下载bin文件,然后改动权限,建链接.配path. 须要这么麻烦吗?我认为不须要. 以下一个命令搞定: forlong401:build forlo ...

  9. 【计算机视觉】基于Kalman滤波器的进行物体的跟踪

    预估器 我们希望能够最大限度地使用測量结果来预计移动物体的运动. 所以,多个測量的累积能够让我们检測出不受噪声影响的部分观測轨迹. 一个关键的附加要素即此移动物体运动的模型. 有了这个模型,我们不仅能 ...

  10. ubuntu下安装RemixOS双系统(Android x86)

    这篇文章主要讲在怎样在ubuntu下安装RemixOS pc版(Android x86版本号),下面两种做法的思路都适合安装不论什么版本号的Android x86版本号到ubuntu系统上,仅仅须要改 ...