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 ...
随机推荐
- vue - config
build/build.js -> config 详细的config配置走向.
- Linux学习笔记 (四)归档和压缩
一.zip压缩命令: 1.压缩文件: 格式:zip 压缩文件 源文件 例:zip abc.zip abc //将abc文件压缩到abc.zip文件内. 2.压缩目录: 格式:zip –r 压缩目录 ...
- libevent2源码分析之五:关键的调用链
用一个调用链来表示函数调用的流程,看起来更直观.根据上面的分析,总结了一些重要的调用链. 初始化 event_base_new event_base_new_with_config min_heap_ ...
- Java: 获取当前执行位置的文件名/类名/方法名/行号
在 JAVA 程序有时需要获取当前代码位置, 于是就利用 Thread.currentThread().getStackTrace() 写了下面这个工具类, 用来获取当前执行位置处代码的文件名/类名/ ...
- JPA学习笔记1——JPA基础 (转自CSDN)
http://blog.csdn.net/chjttony/article/details/6086298 1.JPA简介: Java持久化规范,是从EJB2.x以前的实体Bean(Entity be ...
- Java中super的几种使用方法并与this的差别
1. 子类的构造函数假设要引用super的话,必须把super放在函数的首位. class Base { Base() { System.out.println("Base" ...
- Linux关闭防火墙命令
(1) 重启后永久性生效: 开启: chkconfig iptables on 关闭: chkconfig iptables off (2) 即时生效,重启后失效: 开启: service iptab ...
- Linux----LVM扩容磁盘空间(讲的也很好)
转:https://www.cnblogs.com/tail-f/p/6143085.html
- objective-c的观察者模式
addObserver即添加消息响应函数.postNotificationName即发消息.
- C++避免内存泄漏的一种技巧
C++ Primer 4th中在section 13.5中的U_Ptr就是一种实用的例子 通过计数的方式,并提供自己的抽象类型的Pointer,从而实现内存管理.在一定的范围内还是非常有效的,比如说在 ...