题目链接:http://poj.org/problem?id=1564

题目大意:给定一个整数t,和n个元素组成的集合。求能否用该集合中的元素和表示该整数,如果可以输出所有可行解。1<=n<=12

Sample Input

4 6 4 3 2 2 1 1
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0

Sample Output

Sums of 4:
4
3+1
2+2
2+1+1
Sums of 5:
NONE
Sums of 400:
50+50+50+50+50+50+25+25+25+25
50+50+50+50+50+25+25+25+25+25+25 分析: 代码如下:
 # include<cstdio>
# include<iostream>
# include<algorithm>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int a[],t,n;
bool flag;
int ans[],len; void dfs(int sum,int mark){
int i;
if(sum == ){
flag = true;
printf("%d",ans[]);
for(i=;i<len;i++)
printf("+%d",ans[i]);
printf("\n");
return;
}
if(sum< || mark>=n) return ;
for(i=mark; i<n; i++){
if(i==mark || a[i] != a[i-]){
ans[len++] = a[i];
dfs(sum-a[i],i+);
len--;
}
}
}
int main(){
int i;
while(scanf("%d%d",&t,&n) && n){
int temp = ;
for(i=;i<n;i++){
scanf("%d",&a[i]);
temp += a[i];
}
printf("Sums of %d:\n",t);
if(temp<t){
printf("NONE\n");
continue;
}
flag = false;
len = ;
sort(a,a+n,cmp);
dfs(t,);
if(!flag)
printf("NONE\n"); }
return ;
}
												

POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)的更多相关文章

  1. [poj 2331] Water pipe ID A*迭代加深搜索(dfs)

    Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description ...

  2. POJ 1564 Sum It Up(DFS)

    Sum It Up Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  3. HDU 1258 Sum It Up (DFS)

    Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  4. HDU 1024:Max Sum Plus Plus(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...

  5. Sum It Up---poj1564(dfs)

    题目链接:http://poj.org/problem?id=1564 给出m个数,求出和为n的组合方式:并按从大到小的顺序输出: 简单的dfs但是看了代码才会: #include <cstdi ...

  6. HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏

    Sum It Up Problem Description Given a specified total t and a list of n integers, find all distinct ...

  7. CodeForces 489C Given Length and Sum of Digits... (dfs)

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

  8. HDU 2208 唉,可爱的小朋友(DFS)

    唉,可爱的小朋友 Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. ACM学习历程—ZOJ 3861 Valid Pattern Lock(dfs)

    Description Pattern lock security is generally used in Android handsets instead of a password. The p ...

随机推荐

  1. 【leetcode】编辑距离

    dp方程“ 1.初始化:dp[0][i]=i; dp[j][0]=j; 2.dp[i][j]=         dp[i-1][j-1](相等) dp[i-1][j]+1 ,,dp[i][j-1]+1 ...

  2. 使用DNSAgent拦截特定域名

    开发程序时,为方便测试,需要把本来发往abc.com的数据发到本地. 最简单的方法是直接在程序中修改,把abc.com修改为需要的地址. 但这样提交代码时,容易把调试地址给提交到服务器. 或是嵌入式设 ...

  3. 【SQL】MySQL内置函数中的字符串函数和日期时间函数

    字符串函数 --拼接字符串组成新的字符串 Select concat(‘A’,’B’); --返回字符串长度 Select length(“CAT”) --返回子字符串在字符串中首次出现的位置,没有返 ...

  4. spring boot项目配置文件集合

    表 1. Spring Boot 推荐的基础 POM 文件 名称 说明 spring-boot-starter 核心 POM,包含自动配置支持.日志库和对 YAML 配置文件的支持. spring-b ...

  5. inline-block元素之间出现间隙

    一.问题 这里部分的组成是一个input框和一个a按钮,然后a标签为了设置它的width和height我让他display:inline-block(行内元素以块级元素显示内容).神奇的一幕出现了,两 ...

  6. 模板引擎 Velocity

    模板引擎 Velocity 一.Velocity简介: Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template langu ...

  7. Linux进程管理—信号、定时器

    信号: 1.       信号的作用: 背景: 进程之间通信比较麻烦. 但进程之间又必须通信,比如父子进程之间. 作用: 通知其他进程响应.进程之间的一种通信机制. 信号: 接受信号的进程马上停止,调 ...

  8. android Camera拍照 及 MediaRecorder录像 预览图像差90度

    Camera拍照: 今天做照相机程序,结果写好了发现出问题了,预览的图像差90度.相关源代码如下: Camera.Parameters params = camera.getParameters(); ...

  9. 系统性能优化分析—CPU消耗

    http://blog.csdn.net/longdel/article/details/7296446

  10. ubuntu 14.04 编译安装 nginx

    下载源码包 nginx 地址:http://nginx.org/en/download.html  下载nginx 1.4.7 编译前先安装两个包: 直接编译安装会碰到缺少pcre等问题,这时候只要到 ...