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. 2017.8.1 Noip2018模拟测试赛(十七)

    日期: 八月第一天  总分: 300分  难度: 提高 ~ 省选    得分: 100分(不应该啊!) 题目目录: T1:战争调度 T2:选数 T3:由乃的OJ 赛后心得: MMP,首先第一题花了大概 ...

  2. Tomcat是怎么工作的(2) -- 动手实现山寨版的简单Web Server

    本文先讲解一下Java web server都是怎么工作的.web server也叫HTTP server——顾名思义它是用HTTP协议和客户端交互的.客户端一般就是各种各样的浏览器了.相信所有朋友都 ...

  3. LeetCode OJ——Two Sum

    http://oj.leetcode.com/problems/two-sum/ 求是否存在两个数的和为target,暴力法,两层循环 #include <iostream> #inclu ...

  4. PHP将emoji表情进行过滤

    emoji表情是个麻烦的东西,不仅储存的时候需要处理,而且在PC的显示上需要三方的类库来处理.并且它还是经常更新.... 最近开发新项目的时候明确要求某个字段要过滤emoji表情,在网上找了个方法,亲 ...

  5. 死磕 java同步系列之AQS起篇

    问题 (1)AQS是什么? (2)AQS的定位? (3)AQS的实现原理? (4)基于AQS实现自己的锁? 简介 AQS的全称是AbstractQueuedSynchronizer,它的定位是为Jav ...

  6. Tyvj——P1864 [Poetize I]守卫者的挑战

    来源:http://www.tyvj.cn/p/1864 描述 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过.“我,Niz ...

  7. Boost Asio介绍--之一

    原文:http://www.tuicool.com/articles/YbeYR3 Boost Asio介绍--之一 时间 2014-03-26 17:57:39  CSDN博客 原文  http:/ ...

  8. jenkins发布普通项目、配置自动上线自动部署

    1.以root用户运行jenkins是不专业的 刚开始用jenkins时用jenkins这个普通用户运行程序,始终无法连接到gitlab,报错如下: 先是把修改jenkins上的git路径,将git修 ...

  9. Hdoj 3506 Monkey Party

    Discription Far away from our world, there is a banana forest. And many lovely monkeys live there. O ...

  10. Java并发容器,底层原理深入分析

    ConcurrentHashMap ConcurrentHashMap底层具体实现 JDK 1.7底层实现 将数据分为一段一段的存储,然后给每一段数据配一把锁, 当一个线程占用锁访问其中一个段数据时, ...