本文執行個體講述了Java笛卡爾積算法原理與實作方法。分享給大家供大家參考,具體如下:
笛卡爾積算法的Java實作:
(1)循環内,每次隻有一列向下移一個單元格,就是CounterIndex指向的那列。
(2)如果該列到尾部了,則這列index重置為0,而CounterIndex則指向前一列,相當于進位,把前列的index加一。
(3)最後,由生成的行數來控制退出循環。
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 };
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
最近碰到了一個笛卡爾積的算法要求,比如傳遞過來的參數是"1,3,6,7==4,5,8,9==3,4==43,45,9==35,4",則傳回的是一個list,如[1,4,43,35][1,4][1,35]……,該list包含是4*4*2*4*2=256個元素,現在的思路是這樣的:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DescartesTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str ="1,4";
List result = descartes(str);
System.out.println(result);
}
@SuppressWarnings("rawtypes")
public static List descartes(String str) {
String[] list = str.split("==");
List strs = new ArrayList();
for(int i=0;i
strs.add(Arrays.asList(list[i].split(",")));
}
System.out.println(strs);
int total = 1;
for(int i=0;i
total*=strs.get(i).size();
}
String[] mysesult = new String[total];
int now = 1;
//每個元素每次循環列印個數
int itemLoopNum = 1;
//每個元素循環的總次數
int loopPerItem =1;
for(int i=0;i
List temp = strs.get(i);
now = now*temp.size();
//目标數組的索引值
int index=0;
int currentSize = temp.size();
itemLoopNum = total/now;
loopPerItem = total/(itemLoopNum*currentSize);
int myindex = 0;
for(int j=0;j
//每個元素循環的總次數
for(int k=0;k
if(myindex==temp.size())
myindex=0;
//每個元素每次循環列印個數
for(int m=0;m
mysesult[index]=(mysesult[index]==null?"":mysesult[index]+",")+((String)temp.get(myindex));
index++;
}
myindex++;
}
}
}
return Arrays.asList(mysesult);
}
}
運作結果輸出:
[[1,7],[4,9],[3,4],[43,[35,4]]
[1,35,9,4]
遞歸算法:
public static void fn(List list,String[] arr,String str){
//疊代list
List li = new ArrayList();
for(int i=0;i
//取得目前的數組
if(i==list.indexOf(arr)){
//疊代數組
System.out.println(arr.length);
for(String st : arr){
st = str + st;
if(i
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程式設計有所幫助。
總結
如果覺得程式設計之家網站内容還不錯,歡迎将程式設計之家網站推薦給程式員好友。
本圖文内容來源于網友網絡收集整理提供,作為學習參考使用,版權屬于原作者。
如您喜歡交流學習經驗,點選連結加入交流1群:1065694478(已滿)交流2群:163560250