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

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

Java AC 代码:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner; public class Main { static int sum, number; static List<Integer> list ; //存放输入的数据 static List<Integer> path; //结果路径 static List<String> result;//存放结果路径的列表,每次得到结果时,都遍历该列表,看看是否有重复,不重复再往里添加 public static void main(String[] args) { Scanner sc = new Scanner(System.in); while((sum = sc.nextInt()) != 0 && (number = sc.nextInt()) != 0) {
list = new ArrayList<Integer>();
path = new ArrayList<Integer>();
result = new ArrayList<String>(); for(int i = 0; i < number; i++) {
list.add(sc.nextInt());
} for(int i = 0; i < number ; i++) { if(list.get(i) == sum) { //遍历的第一个数(i不一定是0)正好等于sum
boolean flag = true;
for(int j = 0; j < result.size(); j++) { //看结果列表里是否有相同的记过
if(Integer.toString(list.get(i)).equals(result.get(j))) {
flag = false;
break;
}
}
if(flag) {
result.add(Integer.toString(list.get(i))); //没有重复的 就添加进去
}
} else {
for(int j = i + 1; j < number; j++ ) { //i后的每一个数都可以作为第二个数进行遍历(这里应该可以把搜索范围缩小点)
path.add(list.get(i));
dfs(list.get(i), j);
path.remove(path.size() - 1);
}
}
} System.out.println("Sums of " + sum + ":");
if(result.size() > 0){
for(int i = 0; i < result.size(); i++) {
System.out.println(result.get(i));
}
} else {
System.out.println("NONE");
}
}
} public static void dfs(int currentSum, int currentLoc) { //参数是指当前的和 与当前输入列表中的位置 if(currentSum + list.get(currentLoc) == sum) { //得到结果
path.add(list.get(currentLoc));
String res = "";
for(int i = 0; i < path.size() - 1; i++) {
res = res + Integer.toString(path.get(i));
res = res + "+";
}
res = res + Integer.toString(path.get(path.size() - 1)); boolean flag = true;
for(int i = 0; i < result.size(); i++) { //看结果列表里有没有和当前结果重复的
if(res.equals(result.get(i))) {
flag = false;
break;
}
}
if(flag) {
result.add(res);
}
path.remove(path.size() - 1);
return;
} if(currentSum + list.get(currentLoc) > sum) {
return;
} if(currentSum + list.get(currentLoc) < sum) {
for(int i = currentLoc; i < number; i++)
for(int j = i + 1; j < number; j ++) {
path.add(list.get(i)); //这里开始把i写成了currentLoc,对于一些有重复数字的结果就会出错
dfs(currentSum + list.get(i), j);
path.remove(path.size() - 1);
}
}
}
}

poj 1564 Sum It Up(dfs)的更多相关文章

  1. POJ 1564 Sum It Up (DFS+剪枝)

                                                                                                       ...

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

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

  3. 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 ...

  4. 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 ...

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

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

  6. POJ 1564 Sum It Up(DFS)

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

  7. poj 1564 Sum It Up 搜索

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

  8. POJ 1564 经典dfs

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

  9. (深搜)Sum It Up -- poj --1564

    链接: http://poj.org/problem?id=1564 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...

随机推荐

  1. 为什么JPA@Modifying需要@Transactional注解

    在JPA开发中遇到一个很奇怪的问题,@Modifying需要和@Transactional配合使用才能正常使用.如下面代码所示 @Modifying @Transactional @Query(&qu ...

  2. SVN appears to be part of a Subversion 问题心得

    昨天更新了下项目,但同时又增加了一个Java工程,我就在本地单独导出到workspace同目录下:结果第二天提交代码的时候,提示如下错误 svn: E155021: The path 'xxx' ap ...

  3. 惠普服务器DL360G6安装ESXi主机后遗忘密码用u盘重置密码

    惠普服务器DL360G6安装ESXi主机后遗忘密码重置密码 先用rufus制作U盘启动盘,启动盘一定要用惠普专用hpe的esxi版本,否则安装会报错, 下载https://www.iplaysoft. ...

  4. JRE、JDK、JVM 及 JIT 之间有什么不同

    java虚拟机(JVM)     使用java编程语言的主要优势就是平台的独立性.你曾经想知道过java怎么实现平台的独立性吗?对,就是虚拟机,它抽象化了硬件设备,开发者和他们的程序的得以操作系统.虚 ...

  5. 点云网格化算法---MPA

    MPA网格化算法思路 第一步:初始化一个种子三角面.(随机选点,基于该点进行临近搜索到第二点:在基于该线段中点临近搜索到第三点) 图1 第二步:在种子三角面的基础上,进行面片的扩充,利用边的中点进行临 ...

  6. Linux驱动开发8——中断处理

    中断包括软中断和硬中断两种,中断是一种异步I/O机制,即中断可以发生在任意时间点. 1.硬中断 硬件中断包括触发中断和处理中断两部分,而维系两者的是中断号,中断号是一种硬件资源. 1.1.注册和释放中 ...

  7. Django学习之Form表单

    一.Form介绍 普通方式手写注册功能 使用form组件实现注册功能 二.Form那些事儿 1.常用字段与插件 initial error_messages password radioSelect ...

  8. 【工具安装】kali linux 安装教程

    日期:2019-07-14 16:36:21 介绍:使用最新版的 VMware 来安装 kali linux 0x01.下载镜像 首先需要安装 VMware,安装步骤点这里. VMware 安装教程 ...

  9. cocos2dx基础篇(13) 编辑框之二CCEditBox

    [3.x] (1)去掉"CC" (2)设置虚拟键盘的编辑类型 > EditBoxInputMode 变为强枚举 EditBox::EditBoxInputMode // SI ...

  10. 【Qt开发】V4L2 API详解 背景知识 打开设备设置参数

    www.linuxtv.org下,有篇文档详细讲解了V4L2相关知识和体系结构.是V4L2方面最全面的文档.可以通过它学习V4L2的一些思路和想法. http://www.linuxtv.org/do ...