笛卡尔积

public class DescartesProduct {

	/**
	 * 
	 * @Date: 2013-7-1上午10:14:14
	 * @Description: 笛卡尔积
	 * @param words
	 * @return
	 */
	public static String[] descartes(String[][]words){
		String[]arr = words[0];
		for (int i = 1; i < words.length; i++) {
			String[]word = words[i];
			List<String> set = new ArrayList<String>();
			for (String s : word) {
				for (String u : arr) {
					String t=u+" "+s;
					set.add(t);
				}
			}
			arr=set.toArray(arr);
		}
		return arr;
	}
	public static void main(String[] args) {
		
		String[][] a = {{"1","2","3"},{"4","5","6"},{"7","8","9"}};
		String[] b = descartes(a);
		for (String s : b) {
			System.out.println(s);
		}
	}
}

猜你喜欢

转载自itace.iteye.com/blog/1971942