查看: 1204|回复: 0

[Java学习] Java笛卡尔积算法原理与实现方法详解

发表于 2018-1-13 09:31:41

本文实例讲述了Java笛卡尔积算法原理与实现方法。分享给大家供大家参考,具体如下:

笛卡尔积算法的Java实现:

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

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

输出共2*3*4=24行:

  1. aa1 bb1 cc1
  2. aa1 bb1 cc2
  3. aa1 bb1 cc3
  4. aa1 bb1 cc4
  5. aa1 bb2 cc1
  6. aa1 bb2 cc2
  7. aa1 bb2 cc3
  8. aa1 bb2 cc4
  9. aa1 bb3 cc1
  10. aa1 bb3 cc2
  11. aa1 bb3 cc3
  12. aa1 bb3 cc4
  13. aa2 bb1 cc1
  14. aa2 bb1 cc2
  15. aa2 bb1 cc3
  16. aa2 bb1 cc4
  17. aa2 bb2 cc1
  18. aa2 bb2 cc2
  19. aa2 bb2 cc3
  20. aa2 bb2 cc4
  21. aa2 bb3 cc1
  22. aa2 bb3 cc2
  23. aa2 bb3 cc3
  24. 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个元素,现在的思路是这样的:

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. public class DescartesTest {
  5. /**
  6. * 获取N个集合的笛卡尔积
  7. *
  8. * 说明:假如传入的字符串为:"1,2,3==5,6==7,8"
  9. * 转换成字符串数组为:[[1, 2, 3], [5, 6], [7, 8]]
  10. * a=[1, 2, 3]
  11. * b=[5, 6]
  12. * c=[7, 8]
  13. * 其大小分别为:a_length=3,b_length=2,c_length=2,
  14. * 目标list的总大小为:totalSize=3*2*2 = 12
  15. * 对每个子集a,b,c,进行循环次数=总记录数/(元素个数*后续集合的笛卡尔积个数)
  16. * 对a中的每个元素循环次数=总记录数/(元素个数*后续集合的笛卡尔积个数)=12/(3*4)=1次,每个元素每次循环打印次数:后续集合的笛卡尔积个数=2*2个
  17. * 对b中的每个元素循环次数=总记录数/(元素个数*后续集合的笛卡尔积个数)=12/(2*2)=3次,每个元素每次循环打印次数:后续集合的笛卡尔积个数=2个
  18. * 对c中的每个元素循环次数=总记录数/(元素个数*后续集合的笛卡尔积个数)=12/(2*1)=6次,每个元素每次循环打印次数:后续集合的笛卡尔积个数=1个
  19. *
  20. * 运行结果:
  21. * [[1, 2, 3], [5, 6], [7, 8]]
  22. 1,5,7,
  23. 1,5,8,
  24. 1,6,7,
  25. 1,6,8,
  26. 2,5,7,
  27. 2,5,8,
  28. 2,6,7,
  29. 2,6,8,
  30. 3,5,7,
  31. 3,5,8,
  32. 3,6,7,
  33. 3,6,8]
  34. 从结果中可以看到:
  35. a中的每个元素每个元素循环1次,每次打印4个
  36. b中的每个元素每个元素循环3次,每次打印2个
  37. c中的每个元素每个元素循环6次,每次打印1个
  38. *
  39. * @param args
  40. */
  41. public static void main(String[] args) {
  42. // TODO Auto-generated method stub
  43. String str ="1,3,6,7==4,5,8,9==3,4==43,45,8,9==35,4";
  44. List<String> result = descartes(str);
  45. System.out.println(result);
  46. }
  47. @SuppressWarnings("rawtypes")
  48. public static List<String> descartes(String str) {
  49. String[] list = str.split("==");
  50. List<List> strs = new ArrayList<List>();
  51. for(int i=0;i<list.length;i++){
  52. strs.add(Arrays.asList(list[i].split(",")));
  53. }
  54. System.out.println(strs);
  55. int total = 1;
  56. for(int i=0;i<strs.size();i++){
  57. total*=strs.get(i).size();
  58. }
  59. String[] mysesult = new String[total];
  60. int now = 1;
  61. //每个元素每次循环打印个数
  62. int itemLoopNum = 1;
  63. //每个元素循环的总次数
  64. int loopPerItem =1;
  65. for(int i=0;i<strs.size();i++){
  66. List temp = strs.get(i);
  67. now = now*temp.size();
  68. //目标数组的索引值
  69. int index=0;
  70. int currentSize = temp.size();
  71. itemLoopNum = total/now;
  72. loopPerItem = total/(itemLoopNum*currentSize);
  73. int myindex = 0;
  74. for(int j=0;j<temp.size();j++){
  75. //每个元素循环的总次数
  76. for(int k=0;k<loopPerItem;k++){
  77. if(myindex==temp.size())
  78. myindex=0;
  79. //每个元素每次循环打印个数
  80. for(int m=0;m<itemLoopNum;m++){
  81. mysesult[index]=(mysesult[index]==null?"":mysesult[index]+",")+((String)temp.get(myindex));
  82. index++;
  83. }
  84. myindex++;
  85. }
  86. }
  87. }
  88. return Arrays.asList(mysesult);
  89. }
  90. }
复制代码

运行结果输出:

[[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,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 7,4,4,43,4, 7,4,4,45,35, 7,4,4,45,4, 7,4,4,8,35, 7,4,4,8,4, 7,4,4,9,35, 7,4,4,9,4, 7,5,3,43,35, 7,5,3,43,4, 7,5,3,45,35, 7,5,3,45,4, 7,5,3,8,35, 7,5,3,8,4, 7,5,3,9,35, 7,5,3,9,4, 7,5,4,43,35, 7,5,4,43,4, 7,5,4,45,35, 7,5,4,45,4, 7,5,4,8,35, 7,5,4,8,4, 7,5,4,9,35, 7,5,4,9,4, 7,8,3,43,35, 7,8,3,43,4, 7,8,3,45,35, 7,8,3,45,4, 7,8,3,8,35, 7,8,3,8,4, 7,8,3,9,35, 7,8,3,9,4, 7,8,4,43,35, 7,8,4,43,4, 7,8,4,45,35, 7,8,4,45,4, 7,8,4,8,35, 7,8,4,8,4, 7,8,4,9,35, 7,8,4,9,4, 7,9,3,43,35, 7,9,3,43,4, 7,9,3,45,35, 7,9,3,45,4, 7,9,3,8,35, 7,9,3,8,4, 7,9,3,9,35, 7,9,3,9,4, 7,9,4,43,35, 7,9,4,43,4, 7,9,4,45,35, 7,9,4,45,4, 7,9,4,8,35, 7,9,4,8,4, 7,9,4,9,35, 7,9,4,9,4]

递归算法:

  1. public static void fn(List<String[]> list,String[] arr,String str){
  2. //迭代list
  3. List<String> li = new ArrayList<String>();
  4. for(int i=0;i<list.size();i++){
  5. //取得当前的数组
  6. if(i==list.indexOf(arr)){
  7. //迭代数组
  8. System.out.println(arr.length);
  9. for(String st : arr){
  10. st = str + st;
  11. if(i<list.size()-1){
  12. fn(list,list.get(i+1),st);
  13. }else if(i==list.size()-1){
  14. li.add(st);
  15. }
  16. }
  17. }
  18. }
  19. for(int i = 0 ; i < li.size();i++ )
  20. {
  21. System.out.println(li.get(i));
  22. }
  23. }
复制代码

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。



回复

使用道具 举报