题目链接

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

题解:题意是给出一个数字t,然后给出一组数字,在这组数字里面找出和为t的数字,并且按照从大到小输出,每个数字只能使用一次,相同的组只输出一次。如果无解,输出NONE。用DFS解。

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
using namespace std;
//#define LOCAL
int n,len,a[],b[],cnt;
int cmp(int a,int b)
{
return a>b;
}
void dfs(int x,int posa,int sum,int posb)
{
int i;
if(sum>n)return;
if(sum==n)
{
cnt++;
for(i=;i<posb;i++)
{
if(i)printf("+%d",b[i]);
else printf("%d",b[i]);
}
printf("\n");
}
for(i=posa;i<len;i++)
{
b[posb]=a[i];
dfs(a[i],i+,sum+a[i],posb+);
while(i+<len&&a[i]==a[i+])i++;
}
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
//Start
int i;
while(~scanf("%d%d",&n,&len),n+len!=)
{
for(i=;i<len;i++)scanf("%d",&a[i]);
sort(a,a+len,cmp);
printf("Sums of %d:\n",n);
cnt=;
dfs(,,,);
if(!cnt)printf("NONE\n");
}
return ;
}

HDU 1258 Sum It Up(DFS)的更多相关文章

  1. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  2. (step4.3.4)hdu 1258(Sum It Up——DFS)

    题目大意:输入t,n,接下来有n个数组成的一个序列.输出总和为t的子序列 解题思路:DFS 代码如下(有详细的注释): #include <iostream> #include <a ...

  3. HDU 1258 Sum It Up(dfs 巧妙去重)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1258 Sum It Up Time Limit: 2000/1000 MS (Java/Others) ...

  4. hdu 1258 Sum It Up (dfs+路径记录)

    pid=1258">Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. hdu 1258 Sum It Up(dfs+去重)

    题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...

  6. HDU 1258 Sum It Up (DFS)

    Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  7. HDU 1258 Sum It Up

    Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  8. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  9. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

随机推荐

  1. idea中建立maven web项卡在Generating Project in Batch mode

    Maven命令执行到Generating Project in Batch mode 卡住,原因是网络带宽不足问题!需要下载一个约5.1M的xml文件. Maven一般命令:mvn archetype ...

  2. mysql循环插入数据库中数据。

    DELIMITER ;; CREATE PROCEDURE test_insert () BEGIN DECLARE i INT DEFAULT 1; WHILE i<100 DO insert ...

  3. HTML,CSS,JS,JQ

    CSS: <style> <!--属性选择器--> .container input[type="text"][name="txt"]{ ...

  4. CodeForces 712D Memory and Scores

    $dp$,前缀和. 记$dp[i][j]$表示$i$轮结束之后,两人差值为$j$的方案数. 转移很容易想到,但是转移的复杂度是$O(2*k)$的,需要优化,观察一下可以发现可以用过前缀和来优化. 我把 ...

  5. Hibernate5-课程笔记4

    单表查询:   Hibernate是DAO层技术,对数据的使用,查询是最为重要的.Hibernate的查询技术非常强大,支持原始SQL语句查询,支持QBC查询及Hibernate特有的HQL查询. H ...

  6. 笔记本光驱位安装固态硬盘及window系统一些过程记录

    自己的笔记本电脑是13年买的  联想G480 i3  32位 2g内存,配置有点低,呵呵.当初刚毕业问家里要钱买的,到现在后悔没有买好一点的笔记本. 用着用着感觉内存不够用,网上就买了根内存条,买之前 ...

  7. 一个数组分四个ul并且每个ul里边有四个li显示

    <?php $a = $array; for($i=0;$i<4;$i++ ) {?> <ul class="new-hover"> <?php ...

  8. .a与.framework的区别

    库是共享程序代码的方式,一般分为静态库和动态库. 静态库:链接时完整地拷贝至可执行文件中,被多次使用就有多份冗余拷贝. iOS中静态库形式: .a和.framework 动态库:链接时不复制,程序运行 ...

  9. Ansible hostvars

    1.  inventory hosts file 中的server 变量会覆盖group变量. hostvars: { "iaas_name": "test", ...

  10. EM算法及其推广的要点

    1.EM算法是含有隐变量的变量的概率模型极大似然估计或极大后验概率估计的迭代算法,含有隐变量的概率模型的数据表示为$P(Y,Z|\theta)$.这里,$Y$是观测变量的数据,$Z$是隐变量的数据,$ ...