Sum It Up

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5820   Accepted: 2970

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 x 1 , . . . , x n . 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 x 1 , . . . , x n 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 distinct; the same sum cannot 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

Source



思路:因为数量少。能够暴力搜索解决。

DFS。值得注意的地方是去重,我用的是和上一递归pre比較。假设同样则减枝。



#include<iostream>
#include<cstdio> using namespace std; int num[15],ans[15];
int flag,t,n; void dfs(int now,int sum,int cur)
{
if(sum==0)
{
flag=1;
printf("%d",ans[0]);
for(int i=1;i<cur;i++)
{
printf("+%d",ans[i]);
}
printf("\n");
return;
}
else
{
int pre=-1;
for(int i=now;i<n;i++)
{
if(sum>=num[i]&&num[i]!=pre)
{
pre=num[i]; //此处与上一次递归的num[i],即pre,作比較。
ans[cur]=num[i];
dfs(i+1,sum-num[i],cur+1);
}
}
}
} int main()
{
while(scanf("%d%d",&t,&n),n&&t)
{
flag=0;
printf("Sums of %d:\n",t);
for(int i=0;i<n;i++)
scanf("%d",num+i);
dfs(0,t,0); if(!flag)
printf("NONE\n");
} return 0; }

       
不知道。我理解得,对不正确。每条递归路线互不影响。

即一个数组ans[15],并没有什么值得覆盖问题。

POJ 1564 Sum It Up (DFS+剪枝)的更多相关文章

  1. poj 1564 Sum It Up(dfs)

    Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7191   Accepted: 3745 Descrip ...

  2. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  3. poj 1564 Sum It Up (DFS+ 去重+排序)

    http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...

  4. poj 1564 Sum It Up

    题目连接 http://poj.org/problem?id=1564 Sum It Up Description Given a specified total t and a list of n ...

  5. POJ 1564 Sum It Up(DFS)

    Sum It Up Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  6. poj 1564 Sum It Up【dfs+去重】

    Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6682   Accepted: 3475 Descrip ...

  7. poj 1564 Sum It Up 搜索

    题意: 给出一个数T,再给出n个数.若n个数中有几个数(可以是一个)的和是T,就输出相加的式子.不过不能输出相同的式子. 分析: 运用的是回溯法.比较特殊的一点就是不能输出相同的式子.这个可以通过ma ...

  8. 【POJ - 1190】生日蛋糕 (dfs+剪枝)

    Descriptions: 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1 <= i <= M)层蛋糕是半径为 ...

  9. POJ 1564 经典dfs

    1.POJ 1564 Sum It Up 2.总结: 题意:在n个数里输出所有相加为t的情况. #include<iostream> #include<cstring> #in ...

随机推荐

  1. web服务端安全之SQL注入攻击

    一.SQL注入攻击的原理攻击者在HTTP请求中,注入恶意的SQL代码,并在服务端执行.比如用户登录,输入用户名camille,密码 ' or '1'='1 ,如果此时使用参数构造的方式,就会出现 ' ...

  2. 深入理解ajax系列第二篇

    前面的话 在上一篇中,概要地介绍了XHR对象的使用.本文将详细介绍使用XHR对象发送请求的两种方式——GET和POST.下面将以实例的形式来详细说明 GET GET是最常见的请求类型,最常用于向服务器 ...

  3. WSGI剖析

    在一个 HTTP 请求到达服务器时, 服务器接收并调用 web 应用程序解析请求, 产生响应数据并返回给服务器. 这里涉及了两个方面的东西: 服务器(server)和应用程序(application) ...

  4. luogu P3592 [POI2015]MYJ

    题目链接 luogu P3592 [POI2015]MYJ 题解 区间dp 设f[l][r][k]表示区间l到r内最小值>=k的最大收益 枚举为k的位置p,那么包含p的区间答案全部是k 设h[i ...

  5. [LOJ2542][PKUWC2018]随机游走(MinMax容斥+树形DP)

    MinMax容斥将问题转化为求x到S中任意点的最小时间. 树形DP,直接求概率比较困难,考虑只求系数.最后由于x节点作为树根无父亲,所以求出的第二个系数就是答案. https://blog.csdn. ...

  6. Codeforces 1085G(1086E) Beautiful Matrix $dp$+树状数组

    题意 定义一个\(n*n\)的矩阵是\(beautiful\)的,需要满足以下三个条件: 1.每一行是一个排列. 2.上下相邻的两个元素的值不同. 再定义两个矩阵的字典序大的矩阵大(从左往右从上到下一 ...

  7. UVA 2474 - Balloons in a Box 爆搜

    2474 - Balloons in a Box 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&a ...

  8. Push导航栏黑影问题

    解决方法可以在自定义的 TabbarViewController里 viewDidLoad方法里 self.view.backgroundColor = [UIColor whiteColor]; 如 ...

  9. UIAutomator2.0初始

    1. 先直接上样例,谷歌官方Demo: https://github.com/googlesamples/android-testing 2. 一句话说明改动思路 Most importantly, ...

  10. jquery常用写法简单记录

    好久不写东西了......话不多说,主要记录一下,最近做的项目中用到的js的记录(虽然特别特别简单) 一 jquery常用写法记录 jQuery(this).addClass("select ...