L - Sum It Up(DFS)
L - Sum It Up
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
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
Sample Output
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
//一般的bfs,关键的在于同一个位置不能放同样的数。
#include <iostream>
#include <algorithm>
using namespace std; int num[];
int way[];
int T,N;
int all,all_ti; void dfs(int now)
{
if (all==T)
{
int i=;
for (;i<=N;i++)
{
if (way[i]==)
{
cout<<num[i];
break;
}
}
for (i++;i<=N;i++)
{
if (way[i]==)
{
cout<<"+"<<num[i];
}
}
cout<<endl; all_ti++;
way[now]=;
all-=num[now];
}
else
{
int last=-;
for (int j=now;j<=N;j++)
{
if (way[j]== && num[j]+all<=T&&last!=num[j])
{
last=num[j];
way[j]=;
all+=num[j];
dfs(j); }
}
way[now]=;
all-=num[now];
} } int cmp(int x,int y)
{return x>y;} int main()
{ while (cin>>T>>N)
{
if (N==) break;
all=all_ti=;
for (int i=;i<=N;i++)
{
cin>>num[i];
way[i]=;
}
sort(num+,num++N,cmp);
cout<<"Sums of "<<T<<":"<<endl;
dfs();
if (all_ti==) cout<<"NONE"<<endl; }
return ;
}
L - Sum It Up(DFS)的更多相关文章
- POJ 1562(L - 暴力求解、DFS)
油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting ...
- HDOJ(HDU).1258 Sum It Up (DFS)
HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...
- LeetCode Path Sum II (DFS)
题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...
- LeetCode Combination Sum II (DFS)
题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...
- LeetCode Combination Sum III (DFS)
题意: 在1-9这9个数字中选择k个出来,若他们的和为n,则加入答案序列,注意升序. 思路: 用DFS的方式,每次决定一个数字,共决策k次.假设上个决策是第i位为5,那么i+1位的范围就是6-9. c ...
- (step4.3.4)hdu 1258(Sum It Up——DFS)
题目大意:输入t,n,接下来有n个数组成的一个序列.输出总和为t的子序列 解题思路:DFS 代码如下(有详细的注释): #include <iostream> #include <a ...
- hdu1258 Sum It Up (DFS)
Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...
- nyoj 927 The partial sum problem(dfs)
描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choo ...
- HDU 1258 Sum It Up(DFS)
题目链接 Problem Description Given a specified total t and a list of n integers, find all distinct sums ...
随机推荐
- 【CI】系列一:总体环境规划
上周花了点时间把CI环境再次给搞起来了,但是觉得在实体机中总觉得不是很安心,安全性不足,另外没有做备份,安全性.扩展性等都不足,且不好迁移. 因为目前只给了我一台PC及,配置其实也不怎么样.但是却需要 ...
- sqlite3 解决并发读写冲突的问题
#include "stdafx.h" #include "sqlite3.h" #include <iostream> #include < ...
- ajax--百度百科
AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 JavaScript和 ...
- sencha touch结合webservice读取jsonp数据详解
sencha touch读取jsonp数据主要依靠Ext.data.JsonP组件,在mvc的store文件中定义代码如下: Ext.define('eparkapp.store.ParksNearb ...
- MySQL 5.6数据导入报 GTID 相关错误
从阿里云备份数据后还原到本地,用命令行 mysql -uroot -p --default-character-set=<character> -f <dbname> < ...
- 【ODPS】UDF基础
UDF全称User Defined Function,即用户自己定义函数.ODPS提供了非常多内建函数来满足用户的计算需求,同一时候用户还能够通过创建自己定义函数来满足 不同的计算需求. UDF ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- [: ==: unary operator expected 解决方法
之前在写脚本时遇到了这样的错误 “[: ==: unary operator expected” 这是由于做判断的变量值为空导致的. 谷歌出解决方案: 在变量之后加任意字符.例如,要判断变量un是否为 ...
- php使用N层加密eval gzinflate str_rot13 base64 破解方法汇总
php使用N层加密eval gzinflate str_rot13 base64 破解方法汇总 来源:本站转载 作者:佚名 时间:2011-02-14 TAG: 我要投稿 PHP使用eval(gzin ...
- Spring读取配置文件的方式总结
一.基于XML配置的方式 1.使用 PropertyPlaceholderConfigurer - 在 applicationContext.xml 中配置: <context:property ...