zoukankan      html  css  js  c++  java
  • poj 1564 Sum It Up(dfs)

    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);
                    }
            }    
        }
    }
  • 相关阅读:
    Eclipse中的快捷键
    xml文件头文件生成策略以及导入约束条件
    HTTP协议状态代码和错误状态含义的解释
    水了一个前端面试 记下问的东西
    整理的一些PHP面试题目
    Magic Index 寻找数组中A[i]=i的位置(原题转自微信号待字闺中)
    【经典算法】寻找最长01字串(转自待字闺中)
    PHP中不用第三个变量交换两个变量的值
    已知一个数组a[N]来构造数组b[N]的有趣算法题
    MySQL安装后默认自带数据库的作用
  • 原文地址:https://www.cnblogs.com/kkkkkk/p/5535848.html
Copyright © 2011-2022 走看看