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]) //cutcontinue;if(count > sum && arr[i] > 0) return; //cutcount += 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--dfs--The partial sum problem

    /*Name: NYOJ--927--The partial sum problemAuthor: shen_渊 Date: 15/04/17 19:41Description: DFS,和 NYOJ ...

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

  3. poj2362 DFS+剪枝

    题大致做法就是对所有小棒子长度求和sum,sum就是正方形的周长,sum/4就是边长side. 问题就转变为:这堆小棒子能否刚好组合成为4根长度均为side的大棒子 不难了解,小棒子的长度越长,其灵活 ...

  4. [Leetcode][第39题][JAVA][组合总和][回溯][dfs][剪枝]

    [问题描述][中等] [解答思路] 1. 回溯 import java.util.ArrayDeque; import java.util.ArrayList; import java.util.De ...

  5. 【HDU - 1518】Square (经典的dfs + 剪枝)

    题干: Given a set of sticks of various lengths, is it possible to join them end-to-end to form a squar ...

  6. poj2248 DFS+剪枝 or BFS

    传送门 2248:Addition Chains 描述 An addition chain for n is an integer sequence with the following four p ...

  7. DFS(剪枝与优化) - 洛谷 P1361 - 小猫爬山

    DFS(剪枝与优化) - 洛谷 P1361 - 小猫爬山 翰翰和达达饲养了N只小猫,这天,小猫们要去爬山. 经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了(呜咕>_&l ...

  8. 牛客算法周周练17D - 刺客信条(dfs剪枝 | 优先队列 + bfs)

    题目大意: 给出一张n * m的地图,每经过一个建筑都需要一定的时间,而经过ABC要花费100,要求输出从S到E的最短时间. 解题思路: 思路一:dfs剪枝 输入地图的时候用char输入,然后转成in ...

  9. DFS剪枝优化 小猫爬山 数独

    DFS剪枝策略总结 优化搜索顺序 优先搜索分支数少(剩余选择少)的情况 排除等效冗余 若对顺序没有要求 可以将排列转化为组合 可行性剪枝 不合法的情况不进行搜索 最优化剪枝 若当前的"消耗& ...

最新文章

  1. “众声喧哗”中的VR,谁来买单?
  2. 2022.3.28紫薇
  3. 使用Github(目的、基本你概念、注册账号)
  4. CentOS 7.4下使用yum安装MySQL5.7.20 最简单的
  5. Linux下添加普通帐号
  6. button的图文设置
  7. MATLAB偏振光的反射与折射,仿真程序动画作品--光的反射与折射*
  8. JavaScript、Ajax、jQuery全部知识点,5分钟速懂!
  9. Unity3D的四种坐标系
  10. pip不是内部命令问题
  11. JavaSE JDK搭建
  12. 通过IDA Pro生成asm文件
  13. 楼宇跨界智能家居未来前景如何有待考证
  14. 以收音机为例总结分析硬件电路的三个步骤
  15. Load Switch负载开关详解
  16. python灰色关联度分析代码_GRA灰色关联度分析学习 附python代码
  17. 白山数聚蜂巢敏捷集成助力能源企业数字化转型
  18. Linux man指令查询文档设定成中文
  19. 新买的电脑网速慢的解决方案
  20. myeclispe 启动报错Location Type Faceted project metadata file /farming/.settings/org.eclips

热门文章

  1. python中进制chr_python中的chr() 如何返回字符?
  2. 机器人学习--F1TENTH弗吉尼亚大学无人驾驶课程
  3. Python IDLE 如何清屏
  4. Xilinx Platform Cable USB II 下载器驱动安装教程——Win10
  5. Bentley MicroStation V8i安装教程
  6. 【ML】 李宏毅机器学习一:Regression、Gradient Descent(python实现)
  7. VC/MFC如何设置对话框背景颜色
  8. 正则化的通俗解释_干货|深度学习中的正则化技术概述(附Python+keras实现代码)...
  9. 咖啡html代码,HTML5咖啡生成代码动画
  10. 【机器学习】深入理解CatBoost