1. 程式人生 > >一集合指定元素組合的條件,求出集合中有多少對組合,集合的元素不重複使用。

一集合指定元素組合的條件,求出集合中有多少對組合,集合的元素不重複使用。

最近碰到一個問題,記錄一下。問題是:有一個商品集合{a,a,a,b,b,c,c,c,c},組合的條件為1個a+1個b+2個c為一個組合。計算出這個這個集合當中有多少對組合。

主要是用List實現的,實現起來還是挺簡單的。將集合中相同的元素放入到同一個集合當中。然後再把存有相同元素的集合加入到一個型別為List的大集合當中去。讓後對大的集合進行迴圈,得當一個相同元素的一個,然後從條件map中取得這個元素的條件值。然後對這個元素集合進行迴圈,結束條件就和map中取得的條件值一樣。對最裡層集合進行迴圈時,把集合的元素從集合中移除,然後加入到新的集合當中去。對元素集合進行迴圈前對集合的大小進行判斷,判斷集合的size是否達到條件值,如果大於等於條件值則進行迴圈,否則結束整個迴圈。



以下程式碼是一個簡單的實現示例

/**
 * 計算組合 比如有一個數組{a,a,a,b,b,c,c,c,c}
 * 條件為一個a加一個b+兩個c為一個組合,計算陣列當中有幾個組合。(陣列中的元素不能重複使用)。
 * 實現思路:將陣列中相同的元素放入一個數組當中,將原始陣列拆分為多個元素相同的陣列。然後將這些陣列放入到一個列表當中。
 * 然後對這個列表進行迴圈,迴圈中取出當前陣列,對其進行條件判斷,
 * 例如當前迴圈到a陣列,a元素的條件為1,那麼從陣列中取出一個a元素,將其加入到新的列表當中。如果陣列中的元素個數小於條件值,那麼迴圈結束。
 *
 * @author lsh
 *
 */
public class Test {
    private static List<String> strList = new ArrayList<String>();
    /**
     * 條件Map
     */
    private static Map<String, Integer> tjMap = new HashMap<String, Integer>();
    static {
        strList.add("a");
        strList.add("a");
        strList.add("a");
        strList.add("b");
        strList.add("b");
        strList.add("c");
        strList.add("c");
        strList.add("c");
        strList.add("c");
        tjMap.put("a", 1);
        tjMap.put("b", 1);
        tjMap.put("c", 2);
    }

    public static void main(String[] args) {
        // 將相同的元素加入到同一個集合當中去
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        for (String s : strList) {
            List<String> sList = map.get(s);
            if (sList == null) {
                sList = new ArrayList<String>();
                sList.add(s);
                map.put(s, sList);
            } else {
                sList.add(s);
                map.put(s, sList);
            }
        }
        // 將所有的集合加入到一個集合當中
        List<List<String>> tjList = new ArrayList<List<String>>();
        Iterator<Entry<String, List<String>>> iterators = map.entrySet()
                .iterator();
        while (iterators.hasNext()) {
            tjList.add(iterators.next().getValue());
        }
        List<List<String>> zhjgList = new ArrayList<List<String>>();
        boolean istrue = true;
        ok: while (istrue) {
            List<String> list = new ArrayList<String>();
            for (List<String> tList : tjList) {
                if (tList != null && tList.size() > 0) {
                    String name = tList.get(0);
                    Integer tj = tjMap.get(name);
                    if (tList.size() >= tj) {
                        Iterator<String> iterator = tList.listIterator();
                        for (int i = 0; i < tj; i++) {
                            String s = iterator.next();
                            iterator.remove();
                            list.add(s);
                        }
                    } else {
                        istrue = false;
                        break ok;
                    }
                } else {
                    istrue = false;
                    break ok;
                }

            }
            zhjgList.add(list);
        }

        System.out.println(tjList);
        System.out.println(zhjgList);
        check(zhjgList, tjMap);
    }

    public static void check(List<List<String>> zhjgList,
            Map<String, Integer> tjMap) {
        for (List<String> list : zhjgList) {
            boolean issuccess = checktj(list, tjMap);
            if (issuccess) {
                System.out.println("符合條件的組合" + list);
            }
        }
    }

    private static boolean checktj(List<String> list, Map<String, Integer> tjMap) {
        boolean issuccess = false;
        Map<String, Integer> map = test(list);
        Set<Entry<String, Integer>> set = tjMap.entrySet();
        for (Entry<String, Integer> entry : set) {
            Integer sjsl = map.get(entry.getKey());
            if (sjsl != null && sjsl >= entry.getValue()) {
                issuccess = true;
            } else {
                issuccess = false;
            }
        }
        return issuccess;
    }

    private static Map<String, Integer> test(List<String> list) {
        Map<String, Integer> slMap = new HashMap<String, Integer>();
        for (String s : list) {
            Integer ss = slMap.get(s);
            if (ss == null) {
                slMap.put(s, 1);
            } else {
                slMap.put(s, 1 + ss);
            }
        }
        return slMap;
    }

}


執行結果