站点工具


侧边栏

抱歉,您没有权限增加页面
服务器技术:算法:穷举案例

package com.isoftstone.tsl.monitor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class Pop {

    private ArrayList<Element> srcList = new ArrayList<>();
    
    private Map<String, Element[]> resultSet = new HashMap<String, Element[]>();
    
    /
     * 递归次数
     */
    private int loopTimes = 8;
    
    /
     * 目标值
     */
    private int targetNum = 0;
    
    
    public int getLoopTimes() {
        return loopTimes;
    }

    public void setLoopTimes(int loopTimes) {
        this.loopTimes = loopTimes;
    }

    public int getTargetNum() {
        return targetNum;
    }

    public void setTargetNum(int targetNum) {
        this.targetNum = targetNum;
    }
    
    

    public ArrayList<Element> getSrcList() {
        return srcList;
    }

    public void setSrcList(ArrayList<Element> srcList) {
        this.srcList = srcList;
    }



    public class Element implements Comparable<Object> {
        private int id;
        
        private int value;

        public Element(int id, int value){
            this.id = id;
            this.value = value;
        }
        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }
        
        @Override
        public int compareTo(Object o) {
            if (o instanceof Element) {
                Element element = (Element) o;
                if (this.value > element.value) {
                    return 1;
                } else if (this.value == element.value) {
                    return 0;
                } else {
                    return -1;
                }
            }
            return 0;
        }
        
        
    }
    
    public Pop(int[] numbers){
        for (int i = 0; i < numbers.length; i++) {
            Element element = new Element(i, numbers[i]);
            srcList.add(element);
        }
    }
    
    
    public void printIfIsRight(Element[] targetList){
        if (targetList == null || targetList.length == 0) {
            return ;
        }
        int totoal = 0;
        for (int i = 0; i < targetList.length; i++) {
            if(targetList[i] != null)
            totoal+= targetList[i].getValue();
        }
        if (totoal == targetNum) {
            System.out.println("===================find=================");
            Element[]  tempList  = new Element[targetList.length];
            for (int i = 0; i < tempList.length; i++) {
                tempList[i] = targetList[i];
            }
            Arrays.sort(tempList);
            String key = "";
            for (int i = 0; i < tempList.length; i++) {
                System.out.println("id = "+tempList[i].getId());
                key = key +tempList[i].getId()+"-";
                System.out.println(""+tempList[i].getValue());
                
            }
            resultSet.put(key, tempList);
            System.out.println("=======================================");
            
            
        }
        
        
    }
    
    public void  printResult() {
        
        for (Iterator<String> iterator = resultSet.keySet().iterator(); iterator.hasNext();) {
            String key = (String) iterator.next();
            Element[] elements = resultSet.get(key);
            System.out.println("===================find=================");
            System.out.println("ids="+key);
            for (int i = 0; i < elements.length; i++) {
                System.out.println("id="+elements[i].getId());
            
                System.out.println("value="+elements[i].getValue());
                
            }
            System.out.println("=======================================");
        }
    }
    
    public ArrayList<Element> popOne(ArrayList<Element> srcList , Element element){
        ArrayList<Element> newList = new ArrayList<>();
        for (int i = 0; i < srcList.size(); i++) {
            Element e = srcList.get(i);
            if (element.getValue() != e.getValue()) {
                newList.add(e);
            }
        }
        return newList;
        
    }
    
    public void findByLoop(ArrayList<Element> srcList, Element[] targetList, int loopOffset) {
        
        
        for (int i = 0; i < srcList.size(); i++) {
            
            Element element = srcList.get(i);
            
            targetList[loopOffset] = element;
                        
            for (int j = loopOffset+1; j < targetList.length; j++) {
                后面的清空
                targetList[j] = null;
            }
            
            
            
            if (loopOffset < this.loopTimes - 1) {
                
                int nextLoopOffset = loopOffset+1;
                                
                findByLoop(popOne(srcList, element), targetList, nextLoopOffset);
                                
                
            }
            
            if (loopOffset == this.loopTimes-1) {
                printIfIsRight(targetList);
            }
                        
        }
        
        
        
        
    }

    public static void main(String[] args) {
    
        int num = 9;
        Element[] targetList = new Element[num];
        int[] numbers = {343420,349488,388100,398168,425090,429256,510636,518340,553603,574291,609070,614425,
                616740,639664,686264,708628,713200,722258,772328,840200,840880,905098,945724,1195817,1248000,
                1308202,2019308,41280,16850,80000,158300,11000,237956,30000,477120,193730,10200,11100,12250,12500,
                20000,20800,22096,22500,25000,26790,29030,29512,29520,31400,31850,33760,35100,35816,41600,53756,
                55900,66400,78160,86900,97536,97540,99800,104300,124800,135024,154080,156952,157788,196378,206746,
                211180,221406,231692,238302,253420,292428,310550,317160,346360,366850,398650,401150,445058,1530790,
                1712261,351993,238258};
        
        Pop pop = new Pop(numbers);
        pop.setTargetNum(3697636);
        pop.setLoopTimes(num);
        
        pop.findByLoop(pop.getSrcList(), targetList, 0);
        
        pop.printResult();
        System.out.print("end");
    }

}

服务器技术/算法/穷举案例.txt · 最后更改: 2020/06/23 17:16 由 chenjinlian