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 ... 
随机推荐
- 火车票抢票API 根据乘客的车次与座席要求快速订票出票
			火车票抢票API 根据乘客的车次与座席要求快速订票出票:https://www.juhe.cn/docs/api/id/257 1.站站查询 接口地址:http://v.juhe.cn/grabTic ... 
- GDB基本命令(整合)(转)
			directory:添加源文件目录 l src.cpp:line_num可进入文件 如:l src.cpp:10 回车自动重复上一命令 一.gdb调试基本知识a.调试器指示的是将要执行的代码行b.只有 ... 
- iOS编程 手动忽略clang编译器警告
			在iOS开发过程中, 我们可能会碰到一些系统方法弃用, weak.循环引用.不能运行之类的警告. 有代码洁癖的孩子们非常想消除他们, 今天就让我们来一次Fuck 警告.! 首先学会主要的语句 #pra ... 
- JAVA自带监控工具的介绍
			转:http://www.alidw.com/?p=326 相信部分同学可能还是不太了解或者很少使用,这些监控工具是jdk5.0以上才会有的,有部分是liunx特有的. 了解这些工具再做压力测试和调优 ... 
- pydensecrf安装报错1、UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 29: invalid start byte2、 LINK : fatal error LNK1158: 无法运行“rc.exe”     error: command 'D:\\software\\vs2015\\VC\\BIN
			pydensecrf安装报错 1.UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 29: invalid st ... 
- Eclipse3.4以上使用dropins的插件安装方式
			Eclipse3.4以上版本支持使用dropins的插件安装方式,使用方便,共有四种使用方法: 1. 最简单的,直接将jar包放到dropins目录下eclipse/ dropins/ 2. 传 ... 
- Google Chrome 浏览器的检索语言设置
			解决为何从一开始安装Google Chrome 浏览器的时候,使用Google 搜索,结果都是日文的问题. 藏的比较隐蔽,没法在“设置”那里修改. 1.问题:搜索内容均是日文: 2.注意到右边有一个“ ... 
- [原创]如何让freeswitch转发客户端自定义的INFO消息
			如何让freeswitch转发客户端自定义的INFO消息 英文概述: this article is about how to configure freeswitch to forward self ... 
- Knockout JS 演示样例
			五个小样例,来自Knockout JS官方站点. //tutorial 1 //following codes uses to demonstrate observable values and ta ... 
- codeforces #364a Cards
			cf的a题没什么好说到,100的量级,每个人给2张牌,使每个人手中的牌点数相等.保证有一种分配方案. 对每个人,先计算出手中的牌的点数,然后循环两遍拿牌就可以. A. Cards time lim ... 
