(step4.3.4)hdu 1258(Sum It Up——DFS)
题目大意:输入t,n,接下来有n个数组成的一个序列。输出总和为t的子序列
解题思路:DFS
代码如下(有详细的注释):
#include <iostream>
#include <algorithm>
using namespace std; /**
* t: 指定的和
* n: 给出的数的个数
* sign : 用来标记是否有解
* index :结果序列中元素的个数
* a[] :用来存储给出的数
* save[] :用来保存结果序列
*
*/
int t, n;
int a[20];
int save[20];
int index;
int sign; //降序排列
int cmp(const int &a, const int& b) {
return a > b;
} void dfs(int k, int sum) {
//如果当前搜索到的和>指定和
if (sum > t) {
return;
}
//如果当前搜索到的和 == 指定和
if (sum == t) {
sign = 1; //将sign标记为1,表示有解
for (int i = 0; i < index - 1; i++) {
cout << save[i] << "+";
}
cout << save[index - 1] << endl;
return;
} //遍历状态
int last = -1;
for (int i = k + 1; i <= n; i++) {
if (a[i] != last) { //当前的数不能跟上一次搜索的起点的数值一样,不然会造成重复
save[index++] = a[i];//将a[i]放进结果序列中
last = a[i]; //last保存当前搜索的起点
dfs(i, sum + a[i]);
index--;
}
}
} int main() {
int i;
while (cin >> t >> n, t + n) {
index = 0;
sign = 0;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + n + 1, cmp); //降序排序
printf("Sums of %d:\n", t);
dfs(0, 0);
if (!sign) {
cout << "NONE" << endl;
}
}
return 0;
}
(step4.3.4)hdu 1258(Sum It Up——DFS)的更多相关文章
- HDOJ(HDU).1258 Sum It Up (DFS)
HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...
- HDU 1258 Sum It Up(DFS)
题目链接 Problem Description Given a specified total t and a list of n integers, find all distinct sums ...
- HDU 1258 Sum It Up(dfs 巧妙去重)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1258 Sum It Up Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1258 Sum It Up (dfs+路径记录)
pid=1258">Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1258 Sum It Up(dfs+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- HDU 1258 Sum It Up
Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- 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 ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
随机推荐
- SQL Server 读取CSV中的数据
测试: Script: create table #Test ( Name ), Age int, T ) ) BULK INSERT #Test From 'I:\AAA.csv' with( fi ...
- Sybase ASE报错:server Error: 8242, Severity: 16, State: 1
昨天上午,同事反映某系统在执行存储过程的过程中报错了,报错的信息异常如下: 05:00000:00009:2014/06/09 15:45:30.34 server Error: 8242, Seve ...
- sysfs分析
Linux设备模型——设备驱动模型和sysfs文件系统解读 内核版本:2.6.30 1. What is sysfs? 个人理解:sysfs向用户空间展示了驱动设备的层次结构.我们都知道设备和对应 ...
- ngnix 下配置多域名跳转 配置方法
自己申请了多个域名,统一使用 http://goipc.cn/ 格式访问网站 server_name goipc.cn ; server_name www.goipc.cn ; server_name ...
- Scrumworks乱码
要搞敏捷,先找个工具.选了scrumworks5.1.安装完后发现录入汉字乱码. 环境: server:CentOS linux db:mysql5.0 appserver:jboss(scrumwo ...
- iOS常见问题(4)
一.非ARC内存管理问题. 有些同学在创建项目的时候忘记点ARC了,导致一些成员属性都莫名其妙的释放了.然后出现了一系列莫名其妙的错误. 在滚动UITableView的时候出现野指针错误. 一出现这些 ...
- 【BZOJ 1026】 [SCOI2009]windy数
Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? In ...
- OC的类的构造方法
构造方法:用来初始化对象的:首先分解一下创建对象的过程: Person *p = [Person new]; // new方法是alloc 和 init 这两个方法的组合: 完整的创建可用对象的过程: ...
- webview加载本地html
//webView.loadUrl("file:///android_asset/index.html"); 加载assets目录中含有的index.html webView.l ...
- 如何解决Mac与iPhone之间handoff连接问题
首先账户以及设备handoff开关问题不再赘述.主要是昨天发现的一个小技巧 当确认所有设备的iCloud账号统一.蓝牙打开.处在同一WiFi下的前提下,我的iPhone和Mac仍然handoff连接有 ...