poj 1564 Sum It Up(dfs)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 7191 | Accepted: 3745 |
Description
Input
Output
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)的更多相关文章
- POJ 1564 Sum It Up (DFS+剪枝)
...
- poj 1564 Sum It Up (DFS+ 去重+排序)
http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...
- 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 ...
- 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 ...
- poj 1564 Sum It Up【dfs+去重】
Sum It Up Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6682 Accepted: 3475 Descrip ...
- POJ 1564 Sum It Up(DFS)
Sum It Up Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- poj 1564 Sum It Up 搜索
题意: 给出一个数T,再给出n个数.若n个数中有几个数(可以是一个)的和是T,就输出相加的式子.不过不能输出相同的式子. 分析: 运用的是回溯法.比较特殊的一点就是不能输出相同的式子.这个可以通过ma ...
- POJ 1564 经典dfs
1.POJ 1564 Sum It Up 2.总结: 题意:在n个数里输出所有相加为t的情况. #include<iostream> #include<cstring> #in ...
- (深搜)Sum It Up -- poj --1564
链接: http://poj.org/problem?id=1564 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...
随机推荐
- 六、RF中断言关键字使用详解
1.should be equal 和should be not equal :比较两个值相等或不相等 2.should start with 和should not start with :判 ...
- python正则表达式 分割字符串
使用或 标准的正则表达式有小括号,但是python的没有 # -*- coding: utf-8 -*- import sys import re import sys reload(sys) sys ...
- OpenStack 实现技术分解 (6) 通用库 — oslo_log
目录 目录 前文列表 扩展阅读 日志级别 oslolog 初始化设置 DEMO oslolog 的相关配置项 oslolog 的日志级别 oslolog 的使用技巧 推荐使用 LOGdebug 的地方 ...
- JavaScript对象的常用属性及使用
什么是浏览器对象模型? 浏览器对象模型(BOM Browser Object Model)是JavaScript的组成之一,它提供了独立于内容和浏览器窗口进行交互的对象,使用浏览器对象模型可以实现与H ...
- springboot上传文件过大,全局异常捕获,客户端没有返回值
最近在项目里进行全局异常处理时,上传文件超过配置大小,异常被捕获,但是接口直接报500错误,且没有任何返回值. 从后台报错日志来看,异常已经被全局异常处理捕获到了,并且也已经完成响应,为什么前端看不到 ...
- 【Linux 应用编程】基础知识
错误提示 Linux 提供的系统调用API,通常会在失败的时候返回 -1.如果想获取更多更详细的报错信息,需要借助全局变量 errno 和 perror 函数: #include <stdio. ...
- CSS3——对齐 组合选择符 伪类 伪元素 导航栏 下拉菜单
水平&垂直对齐 元素居中对齐 .center { margin: auto; width: 50%; border: 3px solid green; padding: 10px; } 文本 ...
- Matlab——图形绘制——二维平面图形
二维平面图形 hold on ————在已画好的图形上添加新的图形 plot 是绘制一维曲线的基本函数,但在使用此函数之前,我们需先定义曲线上每一点的x 及y 座标.下例可画出一条正弦曲线: > ...
- unity让碰撞只发生一次
碰撞发生在帧的开始,所以你可以检测到冲突,并在LateUpdate复位: private bool hasCollided = false; void OnCollisionEnter(Collisi ...
- Centos快速安装 Memcached
rpm qa|grep memcached //首先检查memcache是否已经安装完成 yum install memcached //(提示你是否确认安装输入y)检查完成后执行安装命令 yum i ...