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 ...
随机推荐
- 问题:今天测试模块一直出现一个问题?module 'subprocess' has no attribute 'Popen'
原因:我起的名字用了模块本身的名字,这个地方一定要切记
- ubuntu 安装wine
笔记 1.安装源 sudo add-apt-repository ppa:wine/wine-builds sudo apt-get update 2.安装wine sudo apt-get inst ...
- [Android]豆瓣FM离线数据
离线目录结构: /sdcard/Android/data/com.douban.radio下 ./cache/fileCaches: 离线音乐歌词(lyric) ./cache/images: 离线音 ...
- vue项目创建流程和使用
vue项目的创建 npm run dev 让项目执行起来 #下载vuex npm install vuex --save#下载axiosnpm install axios --save 当我们生成项目 ...
- 我的Android进阶之旅------>如何将Android源码导入Eclipse中来查看(非常实用)
Android源码下载完成的目录结构如如所示: step1:将.classpath文件拷贝到源代码的根目录 Android源码支持多种IDE,如果是针对APP层做开发的话,建议大家使用Eclipse开 ...
- 使用OpenSSL工具制作X.509证书的方法及其注意事项总结
版权声明:本文为博主原创文章.转载请注明出处. https://blog.csdn.net/Ping_Fani07/article/details/21622545 怎样使用OpenSSL工具生成根证 ...
- mysql 分页测试,
大环境:MySQL5.6 自己造了 27万数据, 一次性 查出来,会超时: 而分页跑,会查出来8s: 但是在少于27万时,直接查比 分页查快5倍:
- 【python】将excel转成json
excel格式如下: 转换后如下 {"BD": 1375.0, "BE": 829.0, "BF": 3.0, "BG" ...
- 关于Unicode转为str的方法
unicode_a=u'\u810f\u4e71' str_a=unicode_a.encode('unicode-escape').decode('string_escape')
- 获取android模拟器的IP地址
http://blog.csdn.net/yjkwf/article/details/7244632 1.输入adb devices查看加载的设备 2.使用 adb -s [设备] [命令]执行命令 ...