Java 組合數(shù)算法

          Posted on 2009-03-05 00:13 logicgate 閱讀(1427) 評(píng)論(0)  編輯  收藏

          今天工作中遇到一個(gè)問(wèn)題需要一個(gè)組合數(shù)算法。google了一會(huì),沒(méi)有找到合適的,于是就自己寫(xiě)了一個(gè)。主要是用了一個(gè)遞歸算法。發(fā)出來(lái)分享一下,有用的著的就拿去,覺(jué)得寫(xiě)得不好的就拍點(diǎn)磚,提點(diǎn)建議。

          ?

          import java.util.*;
          
          public class Combination
          {
          	public static void main(String[] args)
          	{
          		Vector testData = new Vector(Arrays.asList(1, 2, 3, 4, 5, 6));
          		Vector results = getAllCombinations(testData);
          		for(int i=0; i<results.size(); i++)
          			System.out.println(results.elementAt(i));
          	}
          
          	public static Vector getAllCombinations(Vector data)
          	{
          		Vector allCombinations = new Vector();
          		for(int i=1; i<=data.size(); i++)
          		{
          			Vector initialCombination = new Vector();
          			allCombinations.addAll(getAllCombinations(data, i));
          		}
          
          		return allCombinations;
          	}
          
          	public static Vector getAllCombinations(Vector data, int length)
          	{
          		Vector allCombinations = new Vector();
          		Vector initialCombination = new Vector();
          		combination(allCombinations, data, initialCombination, length);
          		return allCombinations;
          	}
          
          	private static void combination(Vector allCombinations, Vector data, 
          		Vector initialCombination, int length)
          	{
          		if(length == 1)
          		{
          			for(int i=0; i<data.size(); i++)
          			{
          				Vector newCombination = new Vector(initialCombination);
          				newCombination.add(data.elementAt(i));
          				allCombinations.add(newCombination);
          			}
          		}
          
          		if(length > 1)
          		{
          			for(int i=0; i<data.size(); i++)
          			{
          				Vector newCombination = new Vector(initialCombination);
          				newCombination.add(data.elementAt(i));
          
          				Vector newData = new Vector(data);
          				for(int j=0; j<=i; j++)
          					newData.remove(data.elementAt(j));
          
          				combination(allCombinations, newData, newCombination, length - 1);
          			}
          		}
          	}
          }

          ?

          測(cè)試結(jié)果:

          [1]
          [2]
          [3]
          [4]
          [5]
          [6]
          [1, 2]
          [1, 3]
          [1, 4]
          [1, 5]
          [1, 6]
          [2, 3]
          [2, 4]
          [2, 5]
          [2, 6]
          [3, 4]
          [3, 5]
          [3, 6]
          [4, 5]
          [4, 6]
          [5, 6]
          [1, 2, 3]
          [1, 2, 4]
          [1, 2, 5]
          [1, 2, 6]
          [1, 3, 4]
          [1, 3, 5]
          [1, 3, 6]
          [1, 4, 5]
          [1, 4, 6]
          [1, 5, 6]
          [2, 3, 4]
          [2, 3, 5]
          [2, 3, 6]
          [2, 4, 5]
          [2, 4, 6]
          [2, 5, 6]
          [3, 4, 5]
          [3, 4, 6]
          [3, 5, 6]
          [4, 5, 6]
          [1, 2, 3, 4]
          [1, 2, 3, 5]
          [1, 2, 3, 6]
          [1, 2, 4, 5]
          [1, 2, 4, 6]
          [1, 2, 5, 6]
          [1, 3, 4, 5]
          [1, 3, 4, 6]
          [1, 3, 5, 6]
          [1, 4, 5, 6]
          [2, 3, 4, 5]
          [2, 3, 4, 6]
          [2, 3, 5, 6]
          [2, 4, 5, 6]
          [3, 4, 5, 6]
          [1, 2, 3, 4, 5]
          [1, 2, 3, 4, 6]
          [1, 2, 3, 5, 6]
          [1, 2, 4, 5, 6]
          [1, 3, 4, 5, 6]
          [2, 3, 4, 5, 6]
          [1, 2, 3, 4, 5, 6]


          已有 3 人發(fā)表留言,猛擊->>這里<<-參與討論


          JavaEye推薦



          主站蜘蛛池模板: 郁南县| 隆尧县| 寿光市| 万盛区| 丹东市| 永清县| 云浮市| 商都县| 金堂县| 白山市| 盐城市| 龙里县| 化隆| 辽源市| 盘山县| 邹城市| 布尔津县| 禹州市| 宣武区| 怀安县| 前郭尔| 新巴尔虎右旗| 车险| 东丽区| 雅安市| 昭觉县| 衡阳县| 台州市| 尼木县| 嘉鱼县| 定南县| 高雄市| 上饶县| 衡水市| 葫芦岛市| 南丹县| 吴忠市| 日照市| 唐河县| 郑州市| 苏州市|