poj 1564 Sum It Up (DFS+ 去重+排序)
http://poj.org/problem?id=1564
该题运用DFS但是要注意去重,不能输出重复的答案
两种去重方式代码中有标出
第一种if(a[i]!=a[i-1])意思是如果这个数a[i]和上一个数相同,那么记录数组的同一个位置就没有必要再放入这个数。例如:4 3 3 2构成和是7,b数组的第二个位置放了3,则后面的那个3就没有必要再放入记录数组的第二个位置了。(可能会放到后面的位置)...
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#define N 110 using namespace std; int a[N], b[N];
int t, n, f; int cmp(const void *a, const void *b)
{
return *(int *)b - *(int *)a;
} void DFS(int k, int j, int sum)
{
int i;
if(sum == t)
{
f = ;
qsort(b, j, sizeof(b[]), cmp); printf("%d", b[]); for(i = ; i < j ; i++)
printf("+%d", b[i]); printf("\n"); return ;
}
for(i = k ; i < n ; i++)
{
if(a[i] != a[i - ] || i == k)
{
b[j] = a[i];
DFS(i + , j + , sum + a[i]); }
/*if(sum + a[i] <= t)
{
b[j] = a[i];
DFS(i + 1, j + 1, sum + a[i]);
while(a[i] == a[i + 1] && i + 1 < n)
i++;//去重
}*/ }
} int main()
{
int i;
while(scanf("%d%d", &t, &n), t + n)
{
f = ;
for(i = ; i < n ; i++)
scanf("%d", &a[i]); qsort(a, n, sizeof(a[]), cmp); printf("Sums of %d:\n", t); DFS(, , ); if(f == )
printf("NONE\n");
}
return ;
}
poj 1564 Sum It Up (DFS+ 去重+排序)的更多相关文章
- POJ 1564 Sum It Up (DFS+剪枝)
...
- 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 ...
- 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 | 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:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- poj 1564 Sum It Up 搜索
题意: 给出一个数T,再给出n个数.若n个数中有几个数(可以是一个)的和是T,就输出相加的式子.不过不能输出相同的式子. 分析: 运用的是回溯法.比较特殊的一点就是不能输出相同的式子.这个可以通过ma ...
- POJ 1564 经典dfs
1.POJ 1564 Sum It Up 2.总结: 题意:在n个数里输出所有相加为t的情况. #include<iostream> #include<cstring> #in ...
- (深搜)Sum It Up -- poj --1564
链接: http://poj.org/problem?id=1564 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...
随机推荐
- Qt之等待提示框(QMovie)
简述 关于gif的使用在实际项目中我用的并不多,因为我感觉瑕疵挺多的,很多时候锯齿比较严重,当然与图存在很大的关系. 关于生成gif的方法可以提供一个网站preloaders,基本是可以满足需求的. ...
- Qt之HTTPS登录
简述 HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTTP ...
- ios多手势事件
开发ios应用时我们经常用到多手势来处理事情,如给scrollView增加点击事件,scrollView不能响应view的touch事件,但有时候却要用到多手势事件,那么我们可以给这个scrollVi ...
- (转载)UITableView的详细讲解
NSIndexPath类型是用来获取用户选择的indexPath,在别的函数里面,若需要知道用户选择了哪个cell,用上它可以省事很多.不必再去建全局变量section和row. NSIndexPat ...
- Windows bat with adb
/********************************************************************* * Windows bat with adb * 说明: ...
- Android之判断某个服务是否正在运行的方法
/** * 判断某个服务是否正在运行的方法 * * @param mContext * @param serviceName * 是包名+服务的类名(例如:net.loonggg.testbackst ...
- define预处理以及宏定义
define的定义方式 无参一般形式:#define 标认符 字符串 比如:#define COUTD "%d\n" 带参一般形式:#def ...
- An unexpected exception occurred while creating a change object. see the error log for more details
今天再给Android项目工程中的包重命名时出现了这个错误(之前重命名的时候就没有出现,郁闷): An unexpected exception occurred while creating a c ...
- SpringMvc+jquery easyui模块开发7步骤
搞了一段java的开发,总结出模块开发经验: SpringMvc+jquery easyui模块开发7步骤:1) 数据表(table): 定义表结构并创建数据表t_use ...
- [Everyday Mathematics]20150222
设 $$\bex a_0=1,\quad a_1=\frac{1}{2},\quad a_{n+1}=\frac{na_n^2}{1+(n+1)a_n}\ (n\geq 1). \eex$$ 试证: ...