一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java基于遞歸和循環兩種方式實現未知維度集合的笛卡爾積算法示例

Java基于遞歸和循環兩種方式實現未知維度集合的笛卡爾積算法示例

2021-02-26 14:28buptdavid Java教程

這篇文章主要介紹了Java基于遞歸和循環兩種方式實現未知維度集合的笛卡爾積算法,結合實例形式分析了Java使用遞歸與循環兩種方式實現未知維度集合的笛卡爾積相關概念、原理與操作技巧,需要的朋友可以參考下

本文實例講述了Java基于遞歸和循環兩種方式實現未知維度集合的笛卡爾積。分享給大家供大家參考,具體如下:

什么是笛卡爾積?

在數學中,兩個集合X和Y的笛卡兒積(Cartesian product),又稱直積,表示為X × Y,第一個對象是X的成員而第二個對象是Y的所有可能有序對的其中一個成員。

假設集合A={a,b},集合B={0,1,2},則兩個集合的笛卡爾積為{(a,0),(a,1),(a,2),(b,0),(b,1), (b,2)}。

如何用程序算法實現笛卡爾積?

如果編程前已知集合的數量,通過程序的多次循環即可得出笛卡爾積。但是如果編程前不知道集合的數量,如何得到笛卡爾積哪?比如集合表示List < List<String>> list;這個list在編程前list的數量是未知的。下面的代碼使用遞歸和循環兩種方法實現未知維度集合的笛卡爾積:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * 循環和遞歸兩種方式實現未知維度集合的笛卡爾積
 * Created on 2015-05-22
 * @author luweijie
 */
public class Descartes {
  /**
   * 遞歸實現dimValue中的笛卡爾積,結果放在result中
   * @param dimValue 原始數據
   * @param result 結果數據
   * @param layer dimValue的層數
   * @param curList 每次笛卡爾積的結果
   */
  private static void recursive (List<List<String>> dimValue, List<List<String>> result, int layer, List<String> curList) {
    if (layer < dimValue.size() - 1) {
      if (dimValue.get(layer).size() == 0) {
        recursive(dimValue, result, layer + 1, curList);
      } else {
        for (int i = 0; i < dimValue.get(layer).size(); i++) {
          List<String> list = new ArrayList<String>(curList);
          list.add(dimValue.get(layer).get(i));
          recursive(dimValue, result, layer + 1, list);
        }
      }
    } else if (layer == dimValue.size() - 1) {
      if (dimValue.get(layer).size() == 0) {
        result.add(curList);
      } else {
        for (int i = 0; i < dimValue.get(layer).size(); i++) {
          List<String> list = new ArrayList<String>(curList);
          list.add(dimValue.get(layer).get(i));
          result.add(list);
        }
      }
    }
  }
  /**
   * 循環實現dimValue中的笛卡爾積,結果放在result中
   * @param dimValue 原始數據
   * @param result 結果數據
   */
  private static void circulate (List<List<String>> dimValue, List<List<String>> result) {
    int total = 1;
    for (List<String> list : dimValue) {
      total *= list.size();
    }
    String[] myResult = new String[total];
    int itemLoopNum = 1;
    int loopPerItem = 1;
    int now = 1;
    for (List<String> list : dimValue) {
      now *= list.size();
      int index = 0;
      int currentSize = list.size();
      itemLoopNum = total / now;
      loopPerItem = total / (itemLoopNum * currentSize);
      int myIndex = 0;
      for (String string : list) {
        for (int i = 0; i < loopPerItem; i++) {
          if (myIndex == list.size()) {
            myIndex = 0;
          }
          for (int j = 0; j < itemLoopNum; j++) {
            myResult[index] = (myResult[index] == null? "" : myResult[index] + ",") + list.get(myIndex);
            index++;
          }
          myIndex++;
        }
      }
    }
    List<String> stringResult = Arrays.asList(myResult);
    for (String string : stringResult) {
      String[] stringArray = string.split(",");
      result.add(Arrays.asList(stringArray));
    }
  }
  /**
   * 程序入口
   * @param args
   */
  public static void main (String[] args) {
    List<String> list1 = new ArrayList<String>();
    list1.add("1");
    list1.add("2");
    List<String> list2 = new ArrayList<String>();
    list2.add("a");
    list2.add("b");
    List<String> list3 = new ArrayList<String>();
    list3.add("3");
    list3.add("4");
    list3.add("5");
    List<String> list4 = new ArrayList<String>();
    list4.add("c");
    list4.add("d");
    list4.add("e");
    List<List<String>> dimValue = new ArrayList<List<String>>();
    dimValue.add(list1);
    dimValue.add(list2);
    dimValue.add(list3);
    dimValue.add(list4);
    List<List<String>> recursiveResult = new ArrayList<List<String>>();
    // 遞歸實現笛卡爾積
    recursive(dimValue, recursiveResult, 0, new ArrayList<String>());
    System.out.println("遞歸實現笛卡爾乘積: 共 " + recursiveResult.size() + " 個結果");
    for (List<String> list : recursiveResult) {
      for (String string : list) {
        System.out.print(string + " ");
      }
      System.out.println();
    }
    List<List<String>> circulateResult = new ArrayList<List<String>>();
    circulate(dimValue, circulateResult);
    System.out.println("循環實現笛卡爾乘積: 共 " + circulateResult.size() + " 個結果");
    for (List<String> list : circulateResult) {
      for (String string : list) {
        System.out.print(string + " ");
      }
      System.out.println();
    }
  }
}

輸出結果是:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
遞歸實現笛卡爾乘積: 共 36 個結果
1 a 3 c
1 a 3 d
1 a 3 e
1 a 4 c
1 a 4 d
1 a 4 e
1 a 5 c
1 a 5 d
1 a 5 e
1 b 3 c
1 b 3 d
1 b 3 e
1 b 4 c
1 b 4 d
1 b 4 e
1 b 5 c
1 b 5 d
1 b 5 e
2 a 3 c
2 a 3 d
2 a 3 e
2 a 4 c
2 a 4 d
2 a 4 e
2 a 5 c
2 a 5 d
2 a 5 e
2 b 3 c
2 b 3 d
2 b 3 e
2 b 4 c
2 b 4 d
2 b 4 e
2 b 5 c
2 b 5 d
2 b 5 e
循環實現笛卡爾乘積: 共 36 個結果
1 a 3 c
1 a 3 d
1 a 3 e
1 a 4 c
1 a 4 d
1 a 4 e
1 a 5 c
1 a 5 d
1 a 5 e
1 b 3 c
1 b 3 d
1 b 3 e
1 b 4 c
1 b 4 d
1 b 4 e
1 b 5 c
1 b 5 d
1 b 5 e
2 a 3 c
2 a 3 d
2 a 3 e
2 a 4 c
2 a 4 d
2 a 4 e
2 a 5 c
2 a 5 d
2 a 5 e
2 b 3 c
2 b 3 d
2 b 3 e
2 b 4 c
2 b 4 d
2 b 4 e
2 b 5 c
2 b 5 d
2 b 5 e

希望本文所述對大家java程序設計有所幫助。

原文鏈接:http://blog.csdn.net/buptdavid/article/details/45918647

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 禁忌第一季第3季 | 国产精品乱码高清在线观看 | 特黄特色大片免费高清视频 | 无限好资源免费观看 | 日韩资源在线 | 亚洲精品有码在线观看 | 国内精品 大秀视频 日韩精品 | 无人区大片免费播放器 | 黑人巨大爆粗亚裔女人 | 韩国成人毛片aaa黄 含羞草国产亚洲精品岁国产精品 | 黄a一级| 2021麻豆剧果冻传媒入口永久 | www.男人的天堂.com | 国产精品麻豆免费版 | 成年男女免费大片在线观看 | 女人把扒开给男人爽的 | 午夜福利理论片高清在线 | 免费日批软件 | 9191久久 | 草草影院国产 | 果冻传媒在线完整免费观 | 国产麻豆精品入口在线观看 | 青草娱乐极品免费视频 | 99r8这里精品热视频免费看 | 别停好爽好深好大好舒服视频 | 亚洲视频日韩 | 韩国一大片a毛片女同 | 国产精品嫩草影院一二三区入口 | 免费日批视频 | 精品国产一二三区在线影院 | 欧美一卡2卡三卡4卡5卡免费观看 | 欧美日韩一区二区综合在线视频 | 久久黄色小视频 | 美女在线看永久免费网址 | 按摩师他揉我奶好爽捏我奶 | 亚洲spank男男实践网站 | 四虎免费影院4hu永久免费 | 99任你躁精品视频 | 精品无人乱码一区二区三区 | 精品一区视频 | 国产主播精品在线 |