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 ...
随机推荐
- nbu还原集群数据库异常问题
集群数据库软件均已安装完毕,现在想从NBU上还原数据库,但在还原控制文件报错 [oracle@oracle-db1 ~]$ rman target / Recovery Manager: Releas ...
- 大哥带的Orchel数据库的注入
0X01 先进行判断 a.jsp?username=SMITH and = 发现单引号闭合 我们尝试构造闭合 存在注入 a.jsp?username=SMITH'='1 正确 a.jsp?user ...
- Oracle开发:常用的数据库字段类型[转]
Oracle常用的数据库字段类型如下: 字段类型 中文说明 限制条件 其它说明 CHAR 固定长度字符串 最大长度2000 bytes VARCHAR2 可变长度的字符串 最大长度4000 bytes ...
- Vue使用axios请求数据,默认post请求传参是json格式,但后台需要formData格式???
最简单的方式,post请求参数json转formData…代码如下: 使用node的 qs 模块(推荐使用) 就是这么简单,在结合element ui表单一键提交涉及到,希望遇到的同学少走弯路,加油~
- Python进制转换format格式化
进制转换:先介绍用传统数学方法,再介绍用python内置方法 二进制转十进制: 1101 转为十进制 1*2^(4-1)+1*2^(3-1)+0*2^(2-1)+1*2^(1-1) 即各个位拆开,乘以 ...
- php语法标识符
php语法标识符 一.总结 一句话总结: 常用<?php //这里写代码 ?>:其它要么不常用,要么需要开配置 二.PHP四大标识符(语法环境) 参考或转自:PHP四大标识符(语法环境) ...
- linux测试某进程占用oi、cpu、内存的使用情况
pidstat 概述 pidstat是sysstat工具的一个命令,用于监控全部或指定进程的cpu.内存.线程.设备IO等系统资源的占用情况.pidstat首次运行时显示自系统启动开始的各项统计信息, ...
- C语言的AES加密
C语言的AES加密 稍微封装了几个函数 方便使用 #if 1 #include <stdio.h> #include <stdlib.h> #include <strin ...
- fiddler之简单的接口性能测试(replay)
在针对某一个/某一些接口,发送相同的请求,不考虑参数的变化时,可以使用fiddler进行简单的性能测试.(使用功能为:replay) 一.replay功能调用 (1.Reissue Requests: ...
- RESR API (一)之Requests
Requests 如果您正在做基于REST的Web服务,您应该忽略request.POST. - Malcom Tredinnick,Django开发团队 REST框架的Request类扩展了标准的H ...