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 题目详情 题解分析 本题是二叉树相关的题目,但是又和路径记录相关. 在记录路径时,可以使用一个栈来存储一条符合的路径,在回溯时将进 ...
随机推荐
- RackTables在LNMP系统的安装及使用
RackTables是一款优秀的机房管理系统,可以十分方便的登记机房设备和连接情况,非常适合小型机房的运维.RackTables是PHP开发的免费系统,最新版本为0.20.14,PHP版本要求不低于P ...
- HR教你面试时怎么谈出高工资
不是任何时候谈钱都会伤感情,比如跟客户谈合同报价,跟房东谈房租,以及面试时和公司HR谈新工作的薪酬待遇. 这事儿一般不需要你先开口.在面试进入尾声的时候,如果HR对你还算满意,通常就会开始问你目前的薪 ...
- Python的前世今生
放弃 拾起 放弃 拾起 最终决定好好的编写一次Python的学习笔记 人生苦短,我用Python --------------Life is short, you need Python 再不抓紧,就 ...
- iOS网络图片缓存SDWebImage
Web image(网络图像) 该库提供了一个支持来自Web的远程图像的UIImageView类别 它提供了: 添加网络图像和缓存管理到Cocoa Touch framework的UIImageVie ...
- linux命令 dig-域名查询工具
博主推荐:更多网络测试相关命令关注 网络测试 收藏linux命令大全 dig命令是常用的域名查询工具,可以用来测试域名系统工作是否正常. 语法 dig(选项)(参数) 选项 @<服务器地址&g ...
- rbac组件之菜单操作(三)
菜单包括菜单列表,菜单列表不仅将菜单列出来,而且将每个菜单下的权限也列出来.菜单的添加.删除.修改. urls.py ... re_path(r'^menus/list/$', MenuView.as ...
- android 如何从activity跳转到另一个activity下指定的fragment
思路: 跳转到目标fragment所在的activity,并传递一个flag,来确定要到哪个fragment,根据该flag判断后,跳转到指定的fragment即可. 代码: 当前界面: intent ...
- http chunked 理解
https://imququ.com/post/transfer-encoding-header-in-http.html #! /usr/bin/python #coding:utf8 import ...
- 立方体 反射 CubeMap
立方体反射(CubeMap) 在虚拟环境中,我们需要模拟材质球反射周围的环境,立方体反射正是让材质球反射出天空盒的背景的方法之一. 原理 当我们观察物体时,物体表面足够光滑,再视线方向和物体相交的点上 ...
- String字符串类的获取功能
StringDemo.java /* * String类的获取功能: * int length():获取字符串的长度,其实也就是字符个数 * char charAt(int index):获取指定索引 ...