笛卡儿积的Java算法实现

本文转载于:http://xiemingmei.iteye.com/blog/1484587

笛卡尔积算法的Java实现:

(1)循环内,每次只有一列向下移一个单元格,就是CounterIndex指向的那列。
(2)如果该列到尾部了,则这列index重置为0,而CounterIndex则指向前一列,相当于进位,把前列的index加一。
(3)最后,由生成的行数来控制退出循环。

1.public class Test {  
2.  
3.    private static String[] aa = { "aa1", "aa2" };  
4.    private static String[] bb = { "bb1", "bb2", "bb3" };  
5.    private static String[] cc = { "cc1", "cc2", "cc3", "cc4" };  
6.    private static String[][] xyz = { aa, bb, cc };  
7.    private static int counterIndex = xyz.length - 1;  
8.    private static int[] counter = { 0, 0, 0 };  
9.  
10.    public static void main(String[] args) throws Exception {  
11.  
12.        for (int i = 0; i < aa.length * bb.length * cc.length; i++) {  
13.            System.out.print(aa[counter[0]]);  
14.            System.out.print("\t");  
15.            System.out.print(bb[counter[1]]);  
16.            System.out.print("\t");  
17.            System.out.print(cc[counter[2]]);  
18.            System.out.println();  
19.  
20.            handle();  
21.        }  
22.    }  
23.  
24.    public static void handle() {  
25.        counter[counterIndex]++;  
26.        if (counter[counterIndex] >= xyz[counterIndex].length) {  
27.            counter[counterIndex] = 0;  
28.            counterIndex--;  
29.            if (counterIndex >= 0) {  
30.                handle();  
31.            }  
32.            counterIndex = xyz.length - 1;  
33.        }  
34.    }  
35.  
36.}  


输出共2*3*4=24行:
aa1 bb1 cc1
aa1 bb1 cc2
aa1 bb1 cc3
aa1 bb1 cc4
aa1 bb2 cc1
aa1 bb2 cc2
aa1 bb2 cc3
aa1 bb2 cc4
aa1 bb3 cc1
aa1 bb3 cc2
aa1 bb3 cc3
aa1 bb3 cc4
aa2 bb1 cc1
aa2 bb1 cc2
aa2 bb1 cc3
aa2 bb1 cc4
aa2 bb2 cc1
aa2 bb2 cc2
aa2 bb2 cc3
aa2 bb2 cc4
aa2 bb3 cc1
aa2 bb3 cc2
aa2 bb3 cc3
aa2 bb3 cc4

笛卡儿积是数学中的一个概念,在计算机科学中,它经常被用来描述不同集合之间所有可能组合的结果。在编程语言中实现笛卡儿积通常涉及嵌套循环来遍历每个集合的所有元素,并将它们组合起来。下面分别以几种不同的编程语言为例,介绍如何实现笛卡儿积。 1. Python语言实现笛卡儿积: ```python def cartesian_product(list1, list2): result = [] for item1 in list1: for item2 in list2: result.append((item1, item2)) return result # 示例 list1 = [1, 2] list2 = ['a', 'b'] print(cartesian_product(list1, list2)) # 输出: [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')] ``` 2. Java语言实现笛卡儿积: ```java import java.util.ArrayList; import java.util.List; public class CartesianProduct { public static List<String> cartesianProduct(List<String> list1, List<String> list2) { List<String> result = new ArrayList<>(); for (String item1 : list1) { for (String item2 : list2) { result.add("(" + item1 + ", " + item2 + ")"); } } return result; } public static void main(String[] args) { List<String> list1 = new ArrayList<>(); List<String> list2 = new ArrayList<>(); list1.add("1"); list1.add("2"); list2.add("a"); list2.add("b"); System.out.println(cartesianProduct(list1, list2)); } } ``` 3. C#语言实现笛卡儿积: ```csharp using System; using System.Collections.Generic; public class CartesianProduct { public static List<string> CartesianProduct(List<string> list1, List<string> list2) { List<string> result = new List<string>(); foreach (var item1 in list1) { foreach (var item2 in list2) { result.Add("(" + item1 + ", " + item2 + ")"); } } return result; } public static void Main(string[] args) { List<string> list1 = new List<string>() { "1", "2" }; List<string> list2 = new List<string>() { "a", "b" }; Console.WriteLine(String.Join("\n", CartesianProduct(list1, list2))); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值