Java基於遞迴和迴圈兩種方式實現未知維度集合的笛卡爾積演算法樣本,維度卡爾

來源:互聯網
上載者:User

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的數量是未知的。下面的代碼使用遞迴和迴圈兩種方法實現未知維度集合的笛卡爾積:

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();    }  }}

輸出結果是:

遞迴實現笛卡爾乘積: 共 36 個結果1 a 3 c1 a 3 d1 a 3 e1 a 4 c1 a 4 d1 a 4 e1 a 5 c1 a 5 d1 a 5 e1 b 3 c1 b 3 d1 b 3 e1 b 4 c1 b 4 d1 b 4 e1 b 5 c1 b 5 d1 b 5 e2 a 3 c2 a 3 d2 a 3 e2 a 4 c2 a 4 d2 a 4 e2 a 5 c2 a 5 d2 a 5 e2 b 3 c2 b 3 d2 b 3 e2 b 4 c2 b 4 d2 b 4 e2 b 5 c2 b 5 d2 b 5 e迴圈實現笛卡爾乘積: 共 36 個結果1 a 3 c1 a 3 d1 a 3 e1 a 4 c1 a 4 d1 a 4 e1 a 5 c1 a 5 d1 a 5 e1 b 3 c1 b 3 d1 b 3 e1 b 4 c1 b 4 d1 b 4 e1 b 5 c1 b 5 d1 b 5 e2 a 3 c2 a 3 d2 a 3 e2 a 4 c2 a 4 d2 a 4 e2 a 5 c2 a 5 d2 a 5 e2 b 3 c2 b 3 d2 b 3 e2 b 4 c2 b 4 d2 b 4 e2 b 5 c2 b 5 d2 b 5 e

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.