hdu1258 Sum It Up (DFS)
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0
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
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std; int a[15],vist[15],b[1000][15],len[1000],bb[15];
int k,su,n,sum,m,flog; bool cmp(int a,int b)
{
return a>b;
}
int findIn_b(int bb[],int m)//是不 是在b数组里面出现,有则表明不用重复输出同一组
{
int i,j;
for(i=0;i<k;i++)
if(m==len[i])
{
for(j=0;j<len[i];j++)
if(bb[j]!=b[i][j])
break;
if(j==len[i])
return 1;
}
return 0;
} void prin_bb(int bb[],int m)//输出
{
int i;
flog=1;//记录有没有输出过
printf("%d",bb[0]);
b[k][len[k]++]=bb[0];//装入,为下一次做对比
for(i=1;i<m;i++)
{
printf("+%d",bb[i]);
b[k][len[k]++]=bb[i];
}
k++;
printf("\n");
} void DFS(int x)
{
int i;
su+=a[x];
bb[m++]=a[x];
vist[x]=1;
if(!findIn_b(bb,m)&&su==sum)
prin_bb(bb,m); if(su<sum)
for(i=x+1;i<n;i++)
if(su+a[i]<=sum&&vist[i]==0)
DFS(i); m--;vist[x]=0;su-=a[x];
}
int main()
{
int i;
while(scanf("%d%d",&sum,&n)>0&&(sum||n))
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);//从大到小排序
flog=0;k=0;m=0;
memset(vist,0,sizeof(vist));
memset(len,0,sizeof(len));
printf("Sums of %d:\n",sum);
for(i=0;i<n;i++)
DFS(i);
if(flog==0)
printf("NONE\n");
}
}
hdu1258 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思想与框架/双 ...
- HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏
Sum It Up Problem Description Given a specified total t and a list of n integers, find all distinct ...
- 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 ...
- 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 ...
- HDU1258 Sum it up
Sum it up 题意:给定一个数sum,和n个数,求sum可以由这n个数里面的那几个数的和表示. Given a specified total t and a list of n integer ...
随机推荐
- Winform使用DevExpress的WaitDialogForm画面
使用了DevExpress的WaitDialogForm 在应用程序加载开始时新建一个线程,并将loading画面show起来,在应用程序画面弹出前将该线程终止. 代码: private DevExp ...
- 数据库,inner join,left join right join 的区别
假设有两个表: 学生和课程 student: class: id student id class studentId 1 ...
- 定义文字用em、rem,效果和px一样
1em=16px font-size: 2.4rem;/*2.4 × 10px = 24px*/
- C#应用程序获取项目路径的方法总结
一.非Web程序 //基目录,由程序集冲突解决程序用来探测程序集 1.AppDomain.CurrentDomain.BaseDirectory //当前工作目录的完全限定路径2.Envi ...
- 局部变量存储区域静态变量存储区域static变量存储区域
局部变量存储区域静态变量存储区域static变量存储区域 常见的存储区域可分为: 1.栈 由编译器在需要的时候分配,在不需要的时候自动清楚的变量的存储区.里面的变量通常是局部变量.函数参数等. 2.堆 ...
- document模板
http://bce.baidu.com/doc/CDS/GettingStarted.html
- 性能相差极大的SQL语句
等价的SQL,性能差异极大,数据库里设计了一个字段存储日期时间,但不是datetime类型,用了时间戳(int 11), 下面有2个SQL语句用于查询数据库,一个是把时间戳转成date进行查询,一个是 ...
- android studio集成环境搭建
1 下载JDK(http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) An ...
- Cortex-M0系统滴答定时器Systick详解
上图是LPC1114系统滴答定时器(SysTick)的结构图.系统滴答定时器位于Cortex-M0内核中,也就是说,不论是LPC1114,还是其他的Cortex-M0内核单片机,都有这个系统定时器.其 ...
- 对于唯一索引使用唯一条件搜索, InnoDB 只锁定找到的index record,不是它之前的区间
| test100 | CREATE TABLE `test100` ( `sn` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增编号', `phoneNo` ...