小編給大家分享一下Java如何實現(xiàn)笛卡爾積算法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
尼河口網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)建站于2013年開始到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運(yùn)維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站。
具體如下:
笛卡爾積算法的Java實現(xiàn):
(1)循環(huán)內(nèi),每次只有一列向下移一個單元格,就是CounterIndex指向的那列。
(2)如果該列到尾部了,則這列index重置為0,而CounterIndex則指向前一列,相當(dāng)于進(jìn)位,把前列的index加一。
(3)最后,由生成的行數(shù)來控制退出循環(huán)。
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
最近碰到了一個笛卡爾積的算法要求,比如傳遞過來的參數(shù)是"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個元素,現(xiàn)在的思路是這樣的:
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class DescartesTest { /** * 獲取N個集合的笛卡爾積 * * 說明:假如傳入的字符串為:"1,2,3==5,6==7,8" * 轉(zhuǎn)換成字符串?dāng)?shù)組為:[[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, * 目標(biāo)list的總大小為:totalSize=3*2*2 = 12 * 對每個子集a,b,c,進(jìn)行循環(huán)次數(shù)=總記錄數(shù)/(元素個數(shù)*后續(xù)集合的笛卡爾積個數(shù)) * 對a中的每個元素循環(huán)次數(shù)=總記錄數(shù)/(元素個數(shù)*后續(xù)集合的笛卡爾積個數(shù))=12/(3*4)=1次,每個元素每次循環(huán)打印次數(shù):后續(xù)集合的笛卡爾積個數(shù)=2*2個 * 對b中的每個元素循環(huán)次數(shù)=總記錄數(shù)/(元素個數(shù)*后續(xù)集合的笛卡爾積個數(shù))=12/(2*2)=3次,每個元素每次循環(huán)打印次數(shù):后續(xù)集合的笛卡爾積個數(shù)=2個 * 對c中的每個元素循環(huán)次數(shù)=總記錄數(shù)/(元素個數(shù)*后續(xù)集合的笛卡爾積個數(shù))=12/(2*1)=6次,每個元素每次循環(huán)打印次數(shù):后續(xù)集合的笛卡爾積個數(shù)=1個 * * 運(yùn)行結(jié)果: * [[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] 從結(jié)果中可以看到: a中的每個元素每個元素循環(huán)1次,每次打印4個 b中的每個元素每個元素循環(huán)3次,每次打印2個 c中的每個元素每個元素循環(huán)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; //每個元素每次循環(huán)打印個數(shù) int itemLoopNum = 1; //每個元素循環(huán)的總次數(shù) int loopPerItem =1; for(int i=0;i<strs.size();i++){ List temp = strs.get(i); now = now*temp.size(); //目標(biāo)數(shù)組的索引值 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++){ //每個元素循環(huán)的總次數(shù) for(int k=0;k<loopPerItem;k++){ if(myindex==temp.size()) myindex=0; //每個元素每次循環(huán)打印個數(shù) for(int m=0;m<itemLoopNum;m++){ mysesult[index]=(mysesult[index]==null?"":mysesult[index]+",")+((String)temp.get(myindex)); index++; } myindex++; } } } return Arrays.asList(mysesult); } }
運(yùn)行結(jié)果輸出:
[[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,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,4,3,8,35, 7,4,3,8,4, 7,4,3,9,35, 7,4,3,9,4, 7,4,4,43,35, 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]
遞歸算法:
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++){ //取得當(dāng)前的數(shù)組 if(i==list.indexOf(arr)){ //迭代數(shù)組 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如何實現(xiàn)笛卡爾積算法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁題目:Java如何實現(xiàn)笛卡爾積算法
網(wǎng)頁URL:http://www.rwnh.cn/article40/igpseo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、全網(wǎng)營銷推廣、虛擬主機(jī)、商城網(wǎng)站、網(wǎng)站設(shè)計公司、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)