题目链接: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. 浅谈pageobject模式

    先来看两段代码 代码1: package com.zlshuo.selenium.nonaming.pageobject; /** * @author leshuo * @version 2014年5 ...

  2. Test2014-3-1 魅力值比较

    魅力值比较 [问题描述] 大学生恋爱的问题造成了数量众多的异地恋,有许多J大的女生早早被Q大男生追走,这导致了J大男生的强烈不满.就在吐血高调地向一位J大美女展开攻势的之后,J大男生终于爆发了. 为了 ...

  3. No valid host was found

    set in nova.conf -> scheduler_default_filters=AllHostsFilter Tried without success.

  4. Java集合类操作优化经验总结

    本文首先针对 Java 集合接口进行了一些介绍,并对这些接口的实现类进行详细描述,包括 LinkedList.ArrayList.Vector.Stack.Hashtable.HashMap.Weak ...

  5. Ubuntu下Django初体验(二)——创建工程及应用

    一.工程目录详解 创建工程后得到如下目录: 1. manage.py 管理项目.创建数据库.启动服务器等.测试等. 查看子命令: python manage.py 启动服务器: python mana ...

  6. modelsim使用命令

    1. 常用仿真命令 vlib work    // 建立work仿真库 vmap work wrok   // 映射库 vlog   -cover  bcest  *.v    // 加覆盖率分析的编 ...

  7. ==和equals()的用法

    先看一段代码: public class TestEqual{ public static void main(String [ ] args){ //基本类型比较 int a = 100; int ...

  8. Node调试工具JSHint

    Node调试工具JSHint的安装及配置教程 作者: 字体:[增加 减小] 类型:转载 时间:2014-05-27我要评论 Node的优势我就不再乱吹捧了,它让javascript统一web的前后台成 ...

  9. 普通身份运行Tomcat

    普通身份运行Tomcat 转载1   权限分配问题 su - username -c “command”这样的形式可以使用任意一个有执行权限的用户执行 -c后边的命令. 注意,- username中间 ...

  10. angular js 表单验证

    <!doctype html> <html ng-app="myapp"> <head> <meta charset="UTF- ...