题目链接:

http://poj.org/problem?id=1564

题目:

Sum It Up
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5839   Accepted: 2984

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

Mid-Central USA 1997

这个题目是典型的dfs。。

我觉得主要的就是重复的数字不需要进行搜索了,因为已经搜索过了,否则会重复。。

当不满足条件时返回上一臣调用处。。

所以代码为:

#include<cstdio>
#include<cstdlib>
const int maxn=100+10;
int a[maxn],b[maxn];
int t,n,ok;
void dfs(int i,int j,int sum)
{int k;if(sum>t)return;if(sum==t){printf("%d",b[1]);for(k=2;k<j;k++)printf("+%d",b[k]);printf("\n");ok=1;return;}for(k=i;k<=n;k++){b[j]=a[k];dfs(k+1,j+1,sum+a[k]);while(a[k]==a[k+1])k++;}
}int main()
{int sum;while(scanf("%d%d",&t,&n)!=EOF){if(t==0&&n==0) return 0;sum=0;for(int i=1;i<=n;i++){scanf("%d",&a[i]);sum=sum+a[i];}printf("Sums of %d:\n",t);ok=0;if(sum<t){printf("NONE\n");continue;}elsedfs(1,1,0);if(!ok)printf("NONE\n");}return 0;
}

poj1564 Sum it up相关推荐

  1. poj1564 Sum It Up dfs水题

    题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...

  2. POJ1564 Sum It Up(DFS)

    题意: 输入一组数,要求输出总数为t的所有组合 要点: 因为数最多只有12个,直接暴力搜索就可以了,还有要判断重复,就是与前一个比较,但是其实只是剪枝提升速度,4,3,2,1,2,1这种还是会输出两个 ...

  3. NUC1399 Sum It Up【DFS】

    Sum It Up 时间限制: 1000ms 内存限制: 65535KB 通过次数: 1总提交次数: 1 问题描述 Given a specified total t and a list of n ...

  4. go build 编译报错 missing go.sum entry for module providing package

    go build 编译报错 missing go.sum entry for module providing package 解决方法 // 移除未使用的依赖 go mod tidy 再次编译,就可 ...

  5. pytorch之expand,gather,squeeze,sum,contiguous,softmax,max,argmax

    目录 gather squeeze expand sum contiguous softmax max argmax gather torch.gather(input,dim,index,out=N ...

  6. C语言计算e1 1/1! 1/2!,c语言程序填空 下面程序是计算sum=1+(1+1/2)+(1+1/2+1/3)+…(1+1/2...

    匿名用户 1级 2016-06-25 回答 <C语言>综合练习题一注意:以下"□"为空格,为回车一.单选题1.阅读以下程序,当输入数据的形式为:25,13,10,正确的 ...

  7. PyTorch 笔记(07)— Tensor 的归并运算(torch.mean、sum、median、mode、norm、dist、std、var、cumsum、cumprod)

    1. Tensor 归并运算函数 此类操作会使输出形状小于输入形状,并可以沿着某一维度进行指定操作,如加法, 既可以计算整个 tensor 的和,也可以计算 tensor 每一行或者 每一列的和, 常 ...

  8. [leetcode] Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  9. usaco ★Zero Sum 和为零

    ★Zero Sum 和为零 请考虑一个由 1 到 N(N=3, 4, 5 ... 9)的数字组成的递增数列:1 2 3 ... N. 现在请在数列中插入"+"表示加,或者" ...

  10. 二叉树:路径之和 Path Sum

    给定一个二叉树与整数sum,找出所有从根节点到叶结点的路径,这些路 径上的节点值累加和为sum 即创建一个二叉树,要求二叉树中有一个路径从根节点到叶节点到路径加起来代表到和为 给定的sum 如下二叉树 ...

最新文章

  1. mgr未同步 mysql_MySQL Group Replication(多主同步复制MGR)
  2. 从Folly源码学C++ 11的新特性
  3. if-else运用及技巧(C# 参考)
  4. postgresql定义访问ip与用户_PostgreSQL 设置允许访问IP的操作
  5. 面试从开始到结束, 必备的一些小技巧
  6. 解构设计!网格表现Logo设计
  7. php项目源码发布linux,php代码上传到linux服务器无法正常显示
  8. POJ1182 食物链【并查集】
  9. HOLOLENS的DEVICE POTAL连接和安装
  10. RubyOnRails终极部署
  11. app架构师实践指南pdf,分享一些行业经验,看完这一篇你就懂了
  12. 什么是Excel宏?
  13. 3DS MAX 导入骨骼动画插件
  14. 获取level2行情接口的功能详解
  15. 微缩脚步趋缓 摩尔定律由于EUV微影技术延迟失去动力
  16. python黑色变白色_在OpenCV python中将白色像素转换为黑色
  17. Windows7使用Programmer Dvorak键盘布局
  18. 2023年徐汇区文化发展专项资金扶持项目申报指南
  19. html根据地点名称查坐标,根据地址查询经纬度Js
  20. 《回炉重造 Java 基础》——集合(容器)

热门文章

  1. Python图片转base64
  2. 关于运行项目时 vue-pdf 插件依赖报错的问题及解决办法
  3. usb2.0 to sata 芯片_达摩院发布业界首款语音合成算法专用AI FPGA芯片设计Ouroboros,效率提高百倍...
  4. C语言随机获取小写字母
  5. 如何选择家庭私有云NAS方案?家庭NAS存储服务器的重要性
  6. 箱形图(python画箱线图)
  7. 如何为IT部门制定更好的战略计划
  8. 现代程序设计 homework-10
  9. Apache FTPServer本地部署FTP服务
  10. 概率论与数理统计大作业实验报告