Problem Description
Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four different sums that equal 4: 4,3+1,2+2, and 2+1+1.(A number
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.
 
Input
The input will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x1,...,xn. If n=0 it signals the end of the input; otherwise, t will be a positive integer
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.
 
Output
For each test case, first output a line containing 'Sums of', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line 'NONE'. The numbers within each sum must appear in nonincreasing order. A number may be repeated
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.
 
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
 
#include<stdio.h>
#include<string.h>
int sum,n;
int a[1010];
int res[1010]={10000};
bool vis[1010];
int j;
bool flag=0;
void DFS(int m,int cnt)
{
int i;//这个i DFS时要用上,且作用非常大,一定得放在里面,我一開始定义了个全局变量,找了快两小时 才找到
if(m==0)
{
flag=1;
for(j=1;j<cnt-1;++j)
printf("%d+",res[j]);
printf("%d\n",res[j]);
return ;
}
for(i=1;i<=n;++i)
{
//
if(!vis[i]&&m-a[i]>=0&&a[i]<=res[cnt-1])
{
vis[i]=1;
res[cnt]=a[i];
DFS(m-a[i],cnt+1);
vis[i]=0;
while(a[i]==a[i+1]&&i<=n) ++i;
}
} }
int main()
{
int i;
while(~scanf("%d%d",&sum,&n),sum+n)
{
flag=0;
for(i=1;i<=n;++i)
{
scanf("%d",a+i);
}
printf("Sums of %d:\n",sum);
memset(vis,0,sizeof(vis));
DFS(sum,1);
if(!flag)printf("NONE\n");
}
return 0;
}

Sum It Up POJ 1564 HDU 杭电1258【DFS】的更多相关文章

  1. 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

    今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...

  2. 一个人的旅行 HDU杭电2066【dijkstra算法 || SPFA】

    pid=2066">http://acm.hdu.edu.cn/showproblem.php? pid=2066 Problem Description 尽管草儿是个路痴(就是在杭电 ...

  3. 『ACM C++』HDU杭电OJ | 1418 - 抱歉 (拓扑学:多面体欧拉定理引申)

    呕,大一下学期的第一周结束啦,一周过的挺快也挺多出乎意料的事情的~ 随之而来各种各样的任务也来了,嘛毕竟是大学嘛,有点上进心的人多多少少都会接到不少任务的,忙也正常啦~端正心态 开心面对就好啦~ 今天 ...

  4. POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)

    题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...

  5. (深搜)Sum It Up -- poj --1564

    链接: http://poj.org/problem?id=1564 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...

  6. hdu杭电1671 / poj3630 字典树

    传送门 题意:输入n串数字 找出是否有存在串的前缀与另一个串相同 如果存在 输出NO否则输出YES 思路:用字典树解决 标记字典树总串的结尾 查找出一个串内部是否有被标记的节点 如果有那么说明存在前缀 ...

  7. 畅通project再续 HDU杭电1875 【Kruscal算法 || Prim】

    Problem Description 相信大家都听说一个"百岛湖"的地方吧.百岛湖的居民生活在不同的小岛中.当他们想去其它的小岛时都要通过划小船来实现.如今政府决定大力发展百岛湖 ...

  8. HDU Today HDU杭电2112【Dijkstra || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=2112 Problem Description 经过锦囊相助,海东集团最终度过了危机,从此.HDU的发展就一直顺风 ...

  9. Choose the best route HDU杭电2680【dijkstra算法 || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

随机推荐

  1. Begin to study Deep Learning

    今天是儿童节,我开始了人生的新的阶段.借助这个节日我想在今年静下心来,简单执着的进行学习,不掺杂任何利益. 早上起来的比较晚,洗了床单吃了早午饭,背着书包就来到了公司.软件园里面很多游客,但是背着电脑 ...

  2. 创建SVN 本地服务器

    svnserve具体配置如下,主要是将 password-db 前的#号去掉,即去掉注释使其生效 passwd具体配置如下,主要是新增自己需要的账号和密码,也可以将原有的账号去掉注释使用 authz ...

  3. DispatcherServlet与ContextLoaderListener的对比

    1. 从DispatcherServlet和ContextLoaderListener的初始化过程可以看出,二者分别会生成一个WebApplicationContext,且以不同的attrName注册 ...

  4. JS中使用EL表达式方法与获取工程名字

    关键: 在js中使用el表达式一定要使用双引号      分两种情况 1. JS代码在JSP页面中, 这可以直接使用EL表达式. 如: <script type="text/javas ...

  5. 36深入理解C指针之---结构体的内存处理

    一.有关结构体的内存处理包括,结构体指针和结构体成员指针的内存分配.结构体成员的数据对齐.结构体的内存释放 1.定义:与自定义数据类型(结构体)有关的内存分配.大小和释放问题 2.特征: 1).用内存 ...

  6. Spring MVC学习一

    SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是DispatcherServlet,DispatcherServlet负责转发每一个Request请 ...

  7. 2017-11-07-noip模拟题

    T1 数学老师的报复 矩阵快速幂模板,类似于菲波那切数列的矩阵 [1,1]*[A,1 B,0] #include <cstdio> #define LL long long inline ...

  8. Xamarin.Forms的基本页面和基本视图

    Xamarin.Forms的基本页面和基本视图   在Xamarin.Forms中,每个App的界面都是一个页面Page.页面的种类有很多种.其中,最常见的页面就是内容页面ContentPage.项目 ...

  9. eclipse 添加 maven 小结

    今天想创建了一个maven项目,由于之前重装系统,maven的一些配置要重新进行配置,y有时候一点小问题那真是一路曲折,为防止之后再遇到此问题,小结一下吧! 环境: eclipse   apache- ...

  10. TdxBarButton的FASTSCRIPT封装

    TdxBarButton的FASTSCRIPT封装 // cxg 2017-2-13 unit fs_dev; interface{$i fs.inc}uses fs_iinterpreter, fs ...