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. NOIP2017 Day-1 模板荟萃

    #include<bits/stdc++.h> #define MAXN 100005 using namespace std; int read(){ ;char c=getchar() ...

  2. wall---向系统当前所有打开的终端上输出信息

    wall命令用于向系统当前所有打开的终端上输出信息.通过wall命令可将信息发送给每位同意接收公众信息的终端机用户,若不给予其信息内容,则wall命令会从标准输入设备读取数据,然后再把所得到的数据传送 ...

  3. QQ音乐

    import re import requestsimport json class Search: def __init__(self, song): ''' self.vkey_url ---&g ...

  4. UVALive 5790 Ball Stacking DP

    DP的方向真的很重要,这题做的时候死活想不出来,看了题解以后恍然大悟原来这么简单. 题意: 有n层堆成金字塔状的球,若你要选一个球,你必须把它上面那两个球取了,当然也可以一个不取.求选的球最大的权值和 ...

  5. Android向unity发送消息

    有些时候需要Android向unity发送消息,有两种方法实现,一.通过unity再带的消息机制,二.通过注册回调的方式. 一.通过UnityPlayer.UnitySendMessage():方法 ...

  6. ActiveMQ学习总结(8)——消息队列设计精要

    消息队列已经逐渐成为企业IT系统内部通信的核心手段.它具有低耦合.可靠投递.广播.流量控制.最终一致性等一系列功能,成为异步RPC的主要手段之一. 当今市面上有很多主流的消息中间件,如老牌的Activ ...

  7. POJ 3265 DP

    思路: f[i][j]表示前i天能做j道题 (是做 不是做完) if(f[i-1][k]) if(suma[j]-suma[k]+g[i-1][k]<=n) f[i][j]=1,g[i][j]= ...

  8. CORS support in Spring Framework--官方

    原文地址:https://spring.io/blog/2015/06/08/cors-support-in-spring-framework For security reasons, browse ...

  9. json.js

    由于json官网被强,现保存源码一份以备不时之需,直接保存成js文件即可. /* json.js 2007-08-05 Public Domain This file adds these metho ...

  10. 安装完Python之后,如何设置Python环境变量

    人生苦短,我用Python.最近有许多加群的萌新在咨询Python安装的事宜,Python安装问题不大,可以戳这篇文章:.本以为安装Python之后就可以万事大吉,高枕无忧了,往命令行中输入pytho ...