Java笛卡尔积算法原理与实现方法详解
本文实例讲述了java笛卡尔积算法原理与实现方法。分享给大家供大家参考,具体如下:
笛卡尔积算法的java实现:
(1)循环内,每次只有一列向下移一个单元格,就是counterindex指向的那列。
(2)如果该列到尾部了,则这列index重置为0,而counterindex则指向前一列,相当于进位,把前列的index加一。
(3)最后,由生成的行数来控制退出循环。
public class test { private static string[] aa = { "aa1", "aa2" }; private static string[] bb = { "bb1", "bb2", "bb3" }; private static string[] cc = { "cc1", "cc2", "cc3", "cc4" }; private static string[][] xyz = { aa, bb, cc }; private static int counterindex = xyz.length - 1; private static int[] counter = { 0, 0, 0 }; public static void main(string[] args) throws exception { for (int i = 0; i < aa.length * bb.length * cc.length; i++) { system.out.print(aa[counter[0]]); system.out.print("\t"); system.out.print(bb[counter[1]]); system.out.print("\t"); system.out.print(cc[counter[2]]); system.out.println(); handle(); } } public static void handle() { counter[counterindex]++; if (counter[counterindex] >= xyz[counterindex].length) { counter[counterindex] = 0; counterindex--; if (counterindex >= 0) { handle(); } counterindex = xyz.length - 1; } } }
输出共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,3,6,7==4,5,8,9==3,4==43,45,8,9==35,4",则返回的是一个list,如[1,4,3,43,35][1,4,3,43,4][1,4,3,45,35]……,该list包含是4*4*2*4*2=256个元素,现在的思路是这样的:
import java.util.arraylist; import java.util.arrays; import java.util.list; public class descartestest { /** * 获取n个集合的笛卡尔积 * * 说明:假如传入的字符串为:"1,2,3==5,6==7,8" * 转换成字符串数组为:[[1, 2, 3], [5, 6], [7, 8]] * a=[1, 2, 3] * b=[5, 6] * c=[7, 8] * 其大小分别为:a_length=3,b_length=2,c_length=2, * 目标list的总大小为:totalsize=3*2*2 = 12 * 对每个子集a,b,c,进行循环次数=总记录数/(元素个数*后续集合的笛卡尔积个数) * 对a中的每个元素循环次数=总记录数/(元素个数*后续集合的笛卡尔积个数)=12/(3*4)=1次,每个元素每次循环打印次数:后续集合的笛卡尔积个数=2*2个 * 对b中的每个元素循环次数=总记录数/(元素个数*后续集合的笛卡尔积个数)=12/(2*2)=3次,每个元素每次循环打印次数:后续集合的笛卡尔积个数=2个 * 对c中的每个元素循环次数=总记录数/(元素个数*后续集合的笛卡尔积个数)=12/(2*1)=6次,每个元素每次循环打印次数:后续集合的笛卡尔积个数=1个 * * 运行结果: * [[1, 2, 3], [5, 6], [7, 8]] 1,5,7, 1,5,8, 1,6,7, 1,6,8, 2,5,7, 2,5,8, 2,6,7, 2,6,8, 3,5,7, 3,5,8, 3,6,7, 3,6,8] 从结果中可以看到: a中的每个元素每个元素循环1次,每次打印4个 b中的每个元素每个元素循环3次,每次打印2个 c中的每个元素每个元素循环6次,每次打印1个 * * @param args */ public static void main(string[] args) { // todo auto-generated method stub string str ="1,3,6,7==4,5,8,9==3,4==43,45,8,9==35,4"; list<string> result = descartes(str); system.out.println(result); } @suppresswarnings("rawtypes") public static list<string> descartes(string str) { string[] list = str.split("=="); list<list> strs = new arraylist<list>(); for(int i=0;i<list.length;i++){ strs.add(arrays.aslist(list[i].split(","))); } system.out.println(strs); int total = 1; for(int i=0;i<strs.size();i++){ total*=strs.get(i).size(); } string[] mysesult = new string[total]; int now = 1; //每个元素每次循环打印个数 int itemloopnum = 1; //每个元素循环的总次数 int loopperitem =1; for(int i=0;i<strs.size();i++){ list temp = strs.get(i); now = now*temp.size(); //目标数组的索引值 int index=0; int currentsize = temp.size(); itemloopnum = total/now; loopperitem = total/(itemloopnum*currentsize); int myindex = 0; for(int j=0;j<temp.size();j++){ //每个元素循环的总次数 for(int k=0;k<loopperitem;k++){ if(myindex==temp.size()) myindex=0; //每个元素每次循环打印个数 for(int m=0;m<itemloopnum;m++){ mysesult[index]=(mysesult[index]==null?"":mysesult[index]+",")+((string)temp.get(myindex)); index++; } myindex++; } } } return arrays.aslist(mysesult); } }
运行结果输出:
[[1, 3, 6, 7], [4, 5, 8, 9], [3, 4], [43, 45, 8, 9], [35, 4]]
[1,4,3,43,35, 1,4,3,43,4, 1,4,3,45,35, 1,4,3,45,4, 1,4,3,8,35, 1,4,3,8,4, 1,4,3,9,35, 1,4,3,9,4, 1,4,4,43,35, 1,4,4,43,4, 1,4,4,45,35, 1,4,4,45,4, 1,4,4,8,35, 1,4,4,8,4, 1,4,4,9,35, 1,4,4,9,4, 1,5,3,43,35, 1,5,3,43,4, 1,5,3,45,35, 1,5,3,45,4, 1,5,3,8,35, 1,5,3,8,4, 1,5,3,9,35, 1,5,3,9,4, 1,5,4,43,35, 1,5,4,43,4, 1,5,4,45,35, 1,5,4,45,4, 1,5,4,8,35, 1,5,4,8,4, 1,5,4,9,35, 1,5,4,9,4, 1,8,3,43,35, 1,8,3,43,4, 1,8,3,45,35, 1,8,3,45,4, 1,8,3,8,35, 1,8,3,8,4, 1,8,3,9,35, 1,8,3,9,4, 1,8,4,43,35, 1,8,4,43,4, 1,8,4,45,35, 1,8,4,45,4, 1,8,4,8,35, 1,8,4,8,4, 1,8,4,9,35, 1,8,4,9,4, 1,9,3,43,35, 1,9,3,43,4, 1,9,3,45,35, 1,9,3,45,4, 1,9,3,8,35, 1,9,3,8,4, 1,9,3,9,35, 1,9,3,9,4, 1,9,4,43,35, 1,9,4,43,4, 1,9,4,45,35, 1,9,4,45,4, 1,9,4,8,35, 1,9,4,8,4, 1,9,4,9,35, 1,9,4,9,4, 3,4,3,43,35, 3,4,3,43,4, 3,4,3,45,35, 3,4,3,45,4, 3,4,3,8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, 7,9,4,9,4]
递归算法:
public static void fn(list<string[]> list,string[] arr,string str){ //迭代list list<string> li = new arraylist<string>(); for(int i=0;i<list.size();i++){ //取得当前的数组 if(i==list.indexof(arr)){ //迭代数组 system.out.println(arr.length); for(string st : arr){ st = str + st; if(i<list.size()-1){ fn(list,list.get(i+1),st); }else if(i==list.size()-1){ li.add(st); } } } } for(int i = 0 ; i < li.size();i++ ) { system.out.println(li.get(i)); } }
更多关于java算法相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。