<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      遞歸和循環(huán)兩種方式實(shí)現(xiàn)未知維度集合的笛卡爾積

      什么是笛卡爾積?

      在數(shù)學(xué)中,兩個(gè)集合X和Y的笛卡兒積(Cartesian product),又稱直積,表示為X × Y,第一個(gè)對象是X的成員而第二個(gè)對象是Y的所有可能有序?qū)Φ钠渲幸粋€(gè)成員。

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

       

      如何用程序算法實(shí)現(xiàn)笛卡爾積?

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

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

      轉(zhuǎn)載 http://blog.csdn.net/buptdavid/article/details/45918647

       

      posted @ 2017-02-17 08:57  一生愛你  閱讀(2532)  評論(1)    收藏  舉報(bào)
      主站蜘蛛池模板: 国产一级精品毛片基地| 久久精品国产亚洲av麻豆小说| 日产国产一区二区不卡| 97se综合| 人妻丝袜AV中文系列先锋影音| 狠狠躁夜夜躁无码中文字幕| 国产午夜福利视频合集| 亚洲免费网站观看视频 | 一区三区在线专区在线| 亚洲国产片一区二区三区| а∨天堂一区中文字幕| 亚洲欧美在线一区中文字幕| 看免费的无码区特aa毛片| 欧美一级黄色影院| 精品不卡一区二区三区| 国产精品成人一区二区三区| 成人免费亚洲av在线| 亚洲一区二区三区播放| 欧美精品一产区二产区| 亚成区成线在人线免费99| 亚洲中文字幕无码专区| 中文字幕在线精品人妻| 亚洲综合av男人的天堂| 久久天天躁综合夜夜黑人鲁色| 日韩有码中文字幕av| 漂亮的人妻不敢呻吟被中出| 亚洲国产中文字幕在线视频综合| 国产成人永久免费av在线| 东京热人妻无码一区二区av| 成人影片一区免费观看| 亚洲区成人综合一区二区| 亚洲av永久无码精品水牛影视| 丁香花成人电影| 国产二区三区不卡免费| 一区二区三区四区黄色网| 精品国产免费人成在线观看| 欧美人成精品网站播放| 国产a在视频线精品视频下载| 九九久久精品国产免费看小说| 鲁一鲁一鲁一鲁一澡| 亚洲AV成人无码久久精品四虎|