博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1258 POJ1564 UVA574 UVALive5319 ZOJ1711 Sum It Up【DFS】
阅读量:6689 次
发布时间:2019-06-25

本文共 3118 字,大约阅读时间需要 10 分钟。

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7926   Accepted: 4068

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 15 3 2 1 1400 12 50 50 50 50 50 50 25 25 25 25 25 250 0

Sample Output

Sums of 4:43+12+22+1+1Sums of 5:NONESums of 400:50+50+50+50+50+50+25+25+25+2550+50+50+50+50+25+25+25+25+25+25

Source

 >> 

问题链接:。

题意简述:参见上文。

问题分析

这个问题是给出一个和,给出若干个正整数,求其哪几组数相加等于指定的和。可以使用DFS来实现。

关键是搜索过程中要去掉重复的解。

DFS属于回溯法,在搜索过程中也可以使用已知条件进行剪枝以加快速度。

程序说明:本程序中,当和的剩余部分小于最小整数值时,不是继续搜索而是回溯,可以加快搜索速度。

AC通过的C语言程序如下:

/* HDU1258 POJ1564 UVA574 UVALive5319 ZOJ1711 Sum It Up */#include 
#include
#define MAXN 15int data[MAXN];int kcount[MAXN];int dsum[MAXN];int ans[MAXN];int kind;int residue;int count;void print_result(){ int i, j, k; for(i=0, j=0; i
=0; i--) { residue -= i * data[k]; if(residue < 0) { ; } else if(residue == 0) { ans[k] = i; count++; print_result(); ans[k] = 0; } else if(residue > 0 && residue >= data[kind-1]) { ans[k] = i; dfs(k+1); ans[k] = 0; } residue += i * data[k]; }}int main(void){ int total, n, sum, i; while(scanf("%d%d", &total, &n) != EOF) { // 判定结束条件 if(total == 0 && n == 0) break; // 读入数据,并求和 sum = 0; for(i=0; i

转载于:https://www.cnblogs.com/tigerisland/p/7564612.html

你可能感兴趣的文章
怎么将图片上传封装成指令?
查看>>
leetcode讲解--861. Score After Flipping Matrix
查看>>
聊聊JavaScript和Scala的表达式 Expression
查看>>
[原]数据科学教程: 如何使用 mlflow 管理数据科学工作流
查看>>
npm上创建发布package
查看>>
解决JS文件引用路径多层查找
查看>>
FE.TEST-前端测试初探
查看>>
超详细Dkhadoop虚拟机安装图文教程
查看>>
排序算法上——冒泡排序、插入排序和选择排序
查看>>
JAVA 8 函数式接口--Supplier
查看>>
Android HTTP
查看>>
Dockerfile多阶段构建原理和使用场景
查看>>
476-数字的补数
查看>>
七牛云赵之健:多维度融合赋能视频 AI 的实践
查看>>
Android 9 Pie震撼来袭 同步登陆WeTest
查看>>
vue+element Form键盘回车事件页面刷新解决
查看>>
CSS3中的box-sizing
查看>>
gracehttp: 优雅重启 Go 程序(热启动 - Zero Downtime)
查看>>
vue-cli中配置全局sass变量
查看>>
云计算新风向:多云战略优化企业云支出
查看>>