题目链接: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. 参考SQLHelper编写的OracleHelper

    使用 Oracle.ManagedDataAccess.Client 类库参考SQLHelper编写的OracleHelper: // ================================ ...

  2. HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  3. bzoj 1036 [ZJOI2008]树的统计Count(树链剖分,线段树)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 10677  Solved: 4313[Submit ...

  4. java解惑

    java对转义字符没有提供任何特殊处理.编译器在将程序解析成各种符号之前,先将 Unicode 转义字符转换成为它们所表示的字符[JLS 3.2] 阅读笔记

  5. android camera(三):camera V4L2 FIMC

    1. V4L2 1)简介 在Linux中,摄像头方面的标准化程度比较高,这个标准就是V4L2驱动程序,这也是业界比较公认的方式. V4L全称是Video for Linux,是Linux内核中标准的关 ...

  6. java-mina(nio 框架)

    mina是对nio的具体实现.是目前比较高效和流行的nio框架了. 下面是对使用mina进行通讯的一个简单demo,后面再用mina写一个RPC的简单框架.   mina主要包括: (使用的mina版 ...

  7. .NET中ToString()的用法

    一.数字转换到字符串 格式说明符     说明       示例                         输出 C                 货币      2.5.ToString(& ...

  8. Socket程序中的Error#10054错误

    近期使用winSock做的一个网络项目中,使用TCP+Socket连接编写的一个多线程的网络程序,功能是client负责不断地向server端发送数据,服务端负责接收数据.client是一个DLL,服 ...

  9. Excel异常Cannot get a text value from a numeric cell

    POI操作Excel时数据Cell有不同的类型,当我们试图从一个数字类型的Cell读取出一个字符串并写入数据库时,就会出现Cannot get a text value from a numeric ...

  10. poj 3294 Life Forms

    后缀数组的题目,把后缀连接起来,这个还是先二分答案,然后选取一段连续的height值,判断这些height代表的后缀有没有覆盖一半以上的字符串. 得出答案的长度之后还要在枚举连续的heigh,判断有没 ...