POJ-1564 dfs
#include"cstring"
#include"cstdio"
const int maxn=+;
int nux[maxn];
int nua[maxn];//解的集合
int t;//t为和
int n;//n为元素个数
bool flag;//判断是否有解
void dfs(int sum,int cur,int k){//sum剩余的大小 k为下标,cur为元素个数
if(sum==){
flag=false;//证明有解
for(int i=;i<cur;i++)
if(i==)
printf("%d",nua[i]);
else
printf("+%d",nua[i]);
printf("\n");
return;
}
for(int i=k;i<n;i++){
if(i==k||nux[i]!=nux[i-]&&sum>=nux[i]){//判断是否重叠
nua[cur]=nux[i];
dfs(sum-nux[i],cur+,i+);
}
}
}
int main(){
while(scanf("%d%d",&t,&n)!=EOF){
if(n==&&t==) break;
for(int i=;i<n;i++){
scanf("%d",&nux[i]);
}
flag=true;
printf("Sums of %d:\n", t);
dfs(t,,);
if(flag)
printf("NONE\n");
}
return ;
}
POJ-1564 dfs的更多相关文章
- POJ 1564 经典dfs
1.POJ 1564 Sum It Up 2.总结: 题意:在n个数里输出所有相加为t的情况. #include<iostream> #include<cstring> #in ...
- poj 1564 Sum It Up (DFS+ 去重+排序)
http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...
- POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)
题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...
- 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 ...
- poj 1564 Sum It Up【dfs+去重】
Sum It Up Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6682 Accepted: 3475 Descrip ...
- POJ 1564 Sum It Up(DFS)
Sum It Up Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- POJ 1564 Sum It Up (DFS+剪枝)
...
- Sum It Up POJ 1564 HDU 杭电1258【DFS】
Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...
- poj 1564 Sum It Up(dfs)
Sum It Up Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7191 Accepted: 3745 Descrip ...
- poj 1564 Sum It Up
题目连接 http://poj.org/problem?id=1564 Sum It Up Description Given a specified total t and a list of n ...
随机推荐
- zendstudio的安装和破解
参考博客地址:http://www.oxox.work/web/php-basic/zendstudio/ 注明:还未验证
- obj-c学习笔记
本文转载至 http://blog.csdn.net/c395565746c/article/details/7573793 当对象经过在dealloc方法处理时,该对象就已经处于已销毁状态,其它 ...
- 九度OJ 1203:IP地址 (字符串处理)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3038 解决:1496 题目描述: 输入一个ip地址串,判断是否合法. 输入: 输入的第一行包括一个整数n(1<=n<=500) ...
- Arrays数组工具类中存在的坑!
以下是一个坑: 看到使用asList时候,可以返回一个集合,当然集合拥有CRUD的特性: 代码中使用 了add和remove时候均报错:Exception in thread "main&q ...
- 【翻译自mos文章】在重建控制文件之前应该考虑的事情
在重建控制文件之前应该考虑的事情 来源于: Things to Consider Before Recreating the Controlfile (文档 ID 1475632.1) 适用于: Or ...
- LRC歌词文件读取代码
/**************************************************/ /*******************-main文件-******************* ...
- Python 列表解析list comprehension和生成表达式generator expression
如果想通过操作和处理一个序列(或其他的可迭代对象)来创建一个新的列表时可以使用列表解析(List comprehensions)和生成表达式(generator expression) (1)list ...
- 每天一个Linux命令(30)ln命令
ln命令用来为文件创建链接,连接类型分为硬链接和符号链接两种,默认的连接类型是硬连接.如果要创建符号连接必须使用"-s"选项. (1)用法: 用法: ln [options ...
- spring项目命名
groupId 一般分为多个段,最简单的分两段,第一段为域,第二段为公司名称.域又分为org.com.cn等等许多, 举个apache公司的tomcat项目例子:这个项目的groupId是org.ap ...
- poj 1144 Network 【求一个网络的割点的个数 矩阵建图+模板应用】
题目地址:http://poj.org/problem?id=1144 题目:输入一个n,代表有n个节点(如果n==0就结束程序运行). 在当下n的这一组数据,可能会有若干行数据,每行先输入一个节点a ...