hdu 1258 Sum It Up (dfs+路径记录)
pid=1258">Sum It Up
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3953 Accepted Submission(s): 2032
can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.
less than 1000, n will be an integer between 1 and 12(inclusive), and x1,...,xn will be positive integers less than 100. All numbers will be separated by exactly one space. The numbers in each list appear in nonincreasing order, and there may be repetitions.
in the sum as many times as it was repeated in the original list. The sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must be sorted by their first number; sums with the same first number
must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. Within each test case, all sums must be distince; the same sum connot appear twice.
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
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
记录答案: 用一个数组跟着搜索路线进行下去,顺便就把答案记录了。
防止答案反复:在一次遍历数组时。记录上一次搜索的值。当前值不和该值相等就好了!
(真是学无止境,继续AC)
#include"stdio.h"
#include"string.h"
#include"math.h"
#include"algorithm"
using namespace std;
#define N 20
int n,t,a[N];
int ans[N],flag;
void dfs(int x,int s,int cnt)
{
int i,tmp;
if(s>t)
return ;
if(s==t)
{
for(i=0;i<cnt;i++)
{
if(i==cnt-1)
printf("%d\n",ans[i]);
else
printf("%d+",ans[i]);
}
flag=1;
}
else
{
tmp=-1;
for(i=x;i<n;i++)
{
if(tmp!=a[i]) //保留当前的数,能避免反复
{
tmp=ans[cnt++]=a[i];
dfs(i+1,s+a[i],cnt);
cnt--;
}
}
}
}
int main()
{
int i;
while(scanf("%d%d",&t,&n),n||t)
{
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Sums of %d:\n",t);
flag=0;
dfs(0,0,0);
if(flag==0)
printf("NONE\n");
}
return 0;
}
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思想与框架/双 ...
- (step4.3.4)hdu 1258(Sum It Up——DFS)
题目大意:输入t,n,接下来有n个数组成的一个序列.输出总和为t的子序列 解题思路:DFS 代码如下(有详细的注释): #include <iostream> #include <a ...
- 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+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- 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 ...
- HDU 1258 Sum It Up
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思想与框架 ...
- 剑指 Offer 34. 二叉树中和为某一值的路径 + 记录所有路径
剑指 Offer 34. 二叉树中和为某一值的路径 Offer_34 题目详情 题解分析 本题是二叉树相关的题目,但是又和路径记录相关. 在记录路径时,可以使用一个栈来存储一条符合的路径,在回溯时将进 ...
随机推荐
- 小写bool和大写BOOL的区别
转:https://blog.csdn.net/ji0525084/article/details/8594469 bool是标准C++中的布尔量,占一个字节大小内存,只有false或者true.具有 ...
- 08C++函数
函数 4.1 概述 一个较大的程序不可能完全由一个人从头至尾地完成,更不可能把所有的内容都放在一个主函数中.为了便于规划.组织.编程和调试,一般的做法是把一个大的程序划分为若干个程序模块(即程序文件) ...
- Myeclipse下载
地址:http://www.jb51.net/softs/593889.html#download
- Oracle存储过程和程序包
一.为什么要用存储过程? 如果在应用程序中经常需要执行特定的操作,可以基于这些操作简历一个特定的过程.通过使用过程可以简化客户端程序的开发和维护,而且还能提高客户端程序的运行性能. 二.过程的优点? ...
- luogu 2-SAT 问题
题目大意:给出n个bool变量,以及m个条件,条件为x,vx,y,vy,表示 x == vx || y == vy . 求匹配. 题解: 最近新学了一下2-SAT算法.2-SAT指有若干个bool变量 ...
- MongoDB数据库的安装
首先就是MongoDB的下载,可以去MongoDB官网进行下载,https://www.mongodb.com/download-center/community,也可以通过百度网盘直接下载, 链接: ...
- 【03】AJAX 向服务器发送请求
AJAX 向服务器发送请求 创建 XMLHttpRequest 对象后,就可以向服务器发送请求了. XMLHttpRequest 对象的 open() 方法和 send() 方法用来向服务器发送请 ...
- Android Studio 使用图片
首先将图片放在drawable下 然后: <ImageView android:layout_width="wrap_content" android:layout_heig ...
- 怎样签发SSL证书
最近在做怎样让网站有SSL,搞了一天,现在总结一下 首先要安装OPENSSL和 Java的 keytool 先用OPENSSL生成私钥和CSR openssl req -newkey rsa:2048 ...
- php.ini中date.timezone设置分析
date.timezone设置php5默认date.timezone为utc,改为date.timezone = PRC即可解决时间相差八小时的问题,但我在php的官方文档中看了半天也没找到这个参数啊 ...