この記事では、Java Cartesian Integration Algorithmの原則と実装方法について説明します。次のように、参照のために共有してください。
デカルト製品アルゴリズムのJava実装:
(1)ループでは、1つの列のみが一度に1つのセルを下に移動します。これは、counterindexによって指し示される列です。
(2)列がテールに到達した場合、この列のインデックスは0にリセットされ、CounterIndexは前の列を指します。
(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、0}; public static void main(string [] args)は例外{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();ハンドル(); }} 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 CC1AA1 BB1 CC2AA1 BB1 CC3AA1 BB1 CC4AA1 BB2 CC1AA1 BB2 CC2AA1 BB2 CC3AA1 BB2 CC4AA1 BB3 CC1AA1 BB3 CC2A1 BB3 CC3A1 BB3 CC4A1 BB1 BB1 BB1 BB1 BB1 BB1 BB1 BB1 BB3 CC4A CC1AA2 BB1 CC2AA2 BB1 CC3AA2 BB1 CC4AA2 BB2 CC1AA2 BB2 CC2AA2 BB2 CC3AA2 BB2 CC4AA2 BB3 CC1AA2 BB3 CC3AA2 BB3 CC4
最近、デカルト製品アルゴリズムの要件に遭遇しました。たとえば、合格したパラメーターは「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,45,35] 4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*。現在のアイデアは次のとおりです。
Java.util.arraylist;インポートJava.util.arrays;インポートJava.util.list; public class descartestest { /** * nセットのデカルト積を取得 * *説明:パスされた文字列が次の場合: "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、 *ターゲットリストの合計サイズは次のとおりです。 a、ループ数=レコード数/(要素数*後続セットのデカルト製品数)= 12/(3*4)= 1時間、各要素数はループごとに印刷されます:後続セットのデカルト製品数= 2*2*bの各要素の各要素の場合、loopsの各要素*) 3回、各ループの数は要素ごとに印刷されます:後続のセットのデカルト製品の数= 2 * c = c =総数の要素数のループ数 /(要素の数 *(後続セットのデカルト積)= 12 /(2 * 1)= 6回、各要素はループごとに印刷されます。 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]は、それぞれの要素にfを把握できます。要素あたり6回の要素6回、毎回1 * * @param args */ public static void main(string [] args){// todo auto-fienated method string str = "1,3,6,7 == 4,5,8,9 == 3,4 == 43,45,8,8,9 == 35,4";リスト<文字列> result = descartes(str); system.out.println(result); } @suppresswarnings( "rawTypes")public static list <string> descartes(string str){string [] list = str.split( "==");リスト<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; //要素ごとのプリント数int itemloopnum = 1; //要素あたりのループの総数int loopperitem = 1; for(int i = 0; i <strs.size(); i ++){list temp = strs.get(i); now = now*temp.size(); //ターゲット配列のインデックス値はint index = 0です。 int currentsize = temp.size(); itemloopnum = total/now; loopperitem = total/(itemloopnum*curlentsize); int myindex = 0; for(int j = 0; j <temp.size(); j ++){//要素あたりのループの総数は(int k = 0; k <loopperitem; k ++){if(myindex == temp.size()myindex = 0; //ループあたりの各要素のプリント数(int m = 0; m <itemloopnum; m ++){mysesult [index] =(mysesesult [index] == null? "":mysesult [index]+"、")+((string)temp.get(myindex));インデックス++; } myindex ++; }}} return arrays.aslist(mysesult); }}結果出力を実行します:
[[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,4、1,4,3,9,35、1,4,3,9,4、1,4,4,43,35、1,4,4,4,4,43,4,4,43,43,4,43,35、 1,4,4,4,4,4,4、1,4,4,8,35、1,4,4,4,9,35、1,4,4,9,4、1,5,3,43,4、1,5,3,43,4、1,5,3,45、1,5,3,43,4、1,5,3,45、1,5,55,35,55,35,55,55,45、 1,5,3,45、1,5,3,45、1,5,3,45、1,5,3,45、1,5,3,45、1,5,45、1,5,3,45、1,5,45、1,5,3,45、1,5,3,45、1,5,3,45、1,5,55,35,55,55、 1,5,3,45、1,5,3,45、1,5,3,45、1,5,3,45、1,5,3,45、1,5,45、1,5,3,45、1,5,45、1,5,3,45、1,5,3,45、1,5,3,45、1,5,55,35,55,55、 1,5,3,45、1,5,3,8,35、1,5、3,8、4、1,5,3,9,35、1,5、3,9,4、1,5,4,43,35、1,5,4,43,4、1,5,4,45,35、1,5,45,4、1,5,5,8,8,8,5,43,43,4,43,4、 1,5,4,9,35、1,5,4,9,4、1,8、3,43,35、1,8,3,45、1,8,3,45、1,8,3,45、1,8,3,45、1,8,3,45、1,8,3,8,4、1,8,8,4、1,8,8,4,4,45、 1,8,3,9,35、1,8,3,9,35、1,8,3,35、35、1,8,3,9,35、1,8,3,35、1,8,3,9,35、1,8,3,9,35、1,8,3,9,35、1,8,3,9,35、335、1,8,335、 1,8,4,43,35、1,8,4,43,4、1,8、45,35、1,8,4,45,4、1,8,4,8,4,8,4、1,8,4,8,4、1,8,4,9,35、1,8,4,4,9,4、1,9,3,43,4、1,9,4、1,9,4、1,43,4、4,9,4、 1,9,3,45、1,9,3,45、1,9,3,8,4、1,9,3,9,4、1,9,4,43,4、1,9,4,45,35、1,9,3,4、1,9,3,9,4、1,9,4,43,4、1,94,43,43,43,4、1,94,43,5,43,43,4、 1,9,4,45,4、3,4,4,4,35、3,4,4,4,4,35、 3,4,3,4,3,4,3,4,3,4,3,4,4,3,4,3,4,3,4,3,3,4,3,4,3,4,3,3,4,3,4,3,4,4,3,4,4,4,3,4,4,4,3,4,4,3,4,4,4,4,4,4,4,4,4,4,3,3,4,3,4,4,4,4,4,3,3,4,3,4,3,3,4,4,3,4,3,3,3,3,4,3,3,4,3 3,4,4,4,4,3,4,4,4,3,4,4,4,4,4,4,4,4,4,3,4,4,4,4,4,4,4,4,3,4,4,4,4,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,3. 、4,4,4,4,4,4,4,4,4,4,3,4,4,4,4,4,4,4,4,4,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4. 3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,35、3,4,4,4,4,4,4,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,35、 3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,35、3,5,3,43,35、3,5,3,43,4、3,5,3,45,35、3,5,3,45,4、3,5,3,8,4、3,5,35,4,3,35,35,4、 3,5,3,9,4、3,5,4,43,35、3,5、43,4、3,5,4,45,4、3,5,4、3,5,4,45,4、3,5,4、3,5,4,9,35、3,5,4、35,4,9,4、3,8,3,3,43,4、3,5,4,4、 3,8,3,45、3,8,3,43,4、3,8,3,45、3,8,3,45、3,8,3,45、3,8,3,45、3,8,3,45、345、3,8,3,45、3,8,3,45、3,8,3,45、4、3,8,35,35 3,8,3,8,4、3,8,3,9,35、3,8,3,9,4、3,8,4,43,4、3,8,43,4、3,8,4,45,35、3,8,4,45,4、3,8,4,45,4、3,8,4,8,4、3,8,45,4、 3,9,3,43,4、3,9,3,45、3,9,3,45、3,9,3,45、3,9,3,45、3,9,3,3,45、3,9,3,3,45、3,9,3,3,45、3,9,3,3,45、3,9,3,3,45、 3,9,3,9,3,3,3,3,3,9,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,9,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3. 、3,9,3,9,3,3,3,9,3,9,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,9,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3. 、3,9,3,9,3,3,3,9,3,9,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,9,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3. 、3,9,3,9,3,3,3,9,3,9,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,9,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3. 3,9,4,43,35、3,9,4,43,4、3,9,45,35、3,9,4,45,4、3,9,4,8,35、3,9,4,8,4,3,9,4,9,4,35、3,9,4,9,4,35、6,4,3,3,43,4,4,9,35,4,9,4,35,4,9,35、 6,4,3,45、6,4,3,45、6,4,3,8、4、6,4,3,9,4、6,4,4,43,35、6,4,4,4,43,4、6,4,4,45,35、6,4,4,4,4,4,4,4,4,4,4,3,35、 6,4,4,4,4,4,4,4,4,4、6,4,4,45,35、45,35、 6,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4、4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4、 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4、4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4、 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4、4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4、 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4、4,4,4,4,4,4,4,4,4,4,4,4,4,4. 6,4,4,8,35、6,4,4,8,4,4,4,4,4,9,35、6,4,4,9,35、6,4,4,4,4,5,3,43,4,6,5,3,43,4,6,5,3,45、6,5,45,45,4,6,5,3,5,3,5,5,5,5,5,5,3,43,43,4,6,5,3,45,5,33,3,43,4,4,6,5,3,3 6,5,3,9,4、6,5,4,43,4、6,5、45,35、6,5、4、4、6,5、4、35、6,5,4,9,35、6,5,4,9,35、6,5、4、4、6,5,4,4,95、6,4,4、6,5,4,4,4,4,35、 6,5,4,8,35、6,5,4,9,35、6,5,4,9,35、6,5、4,9,4、6,8,3,35、6,8,3,43,4、6,8,3,45,35、6,8,3,45,4、6,8,3,8,8,4、6,8,3,3,8,4,4,35,3,35,35,35,35,35,3,35,35,3,35,3,35,3,35. 6,8,3,8,4、6,8,3,9,35、6,8、3,9,4、6,8,4,43,4、6,8,4,45、6,8,4,45、6,8,4,4,9,35、6,8,4,9,4、6,9,3,43、6,9,3,43,4、6,43,4、6,3,43,4、6,3,43,4、6,3,43,4、 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45, 6,9,3,45、6,9,3,45、6,9,3,45、6,9,3,45、6,9,3,45、6,9,3 6,9,3,8,35、6,9,3,8,4、6,9,3,9,35、6,9,35,4,6,9,43,35、6,43,35、6,9,43,35、6,9,43,35、6,9,43,35、 6,9,4,45,35、6,9,4,45,4、6,9,4,4、6,9,4,8,35、6,9,4,4,4、7,4,45、7,4,3,8、4、7,4,3,45、7,4,3,9,4、7,4、7,4、7,4、7,4、7,4、7,4,45、7,4、7,4、7,4、7,4、7,4,45,45 7,4,3,8,4、7,4,3,8,4、7,4,3,9,35、7,4,3,9,4、7,4,3,8,4、7,4,3,4、7,4,4,43,35、7,4,43,43,43,43,43,43,43,43,43,43,43,43,43,43, 7,4,4,4,4,4,4,4,4,8,35、7,4,8,4,4,4,4,4,9,35、7,4,4,9,35、7,4,4,9,35、7,4,4,4,9,4,7,5,3,43,4、7,5,43,43,4、7,5,35,35,35,35,35,5,5,5,5,5,4,9,4,7,5,3,43 7,5,3,45、7,5,3,8、4、7,5、3,9,35、7,5,4,43,4、7,5、43,4、7,5、4,43,4、7,43,4、7,5,4,45,35、7,4,45,4、7,5,4,8,8,35 7,5,4,8,4,7,5,4,8,4,7,5,4,9,35、7,5,4,9,35、7,5,4,9,4,7,8,3,43,4、7,8,3,43,4、7,8,3,45、7,8,33,45、7,8,3,45、7,8,3,45、 7,8,3,8,4、7,8,4,9,35、7,8,4,9,35、7,8,4,9,35、7,8,4,43,4、7,8,4,45、7,8,45、7,8,4,8,4,9,35、7,8,4,9,35、7,8,45,45、7,8,45、7,8,45、7,8,45、 7,8,4,9,35、7,8,4,9,35、7,8、4、4、7,8,4,9,35、7,8,4,4,4、7,8,4,9,35、7,4,4、7,8,4,9,35、7,8,4,4,4,4,4、7,8,4,95、7,8,95、7,8,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4. 7,8,4,9,35、7,8,4,9,4,4、7,9,3,35、7,9,3,43,4、7,9,3,45、35、7,9,3,45,4、7,9,3,8,4、7,9,3,8,4、7,9,3,9,4、7,9,9,9,9,9,9,9,9,4、 7,9,4,43,35、7,9,4,45、7,9,4,45、7,9,4、7,9,4,8,35、7,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,4,9,4,9,4,9,4,9,4,4,9,9,4,9,4,9,4,4,9
再帰アルゴリズム:
public static void fn(list <string []> list、string [] arr、string str){// iterate list <string> li = new arraylist <string>(); for(int i = 0; i <list.size(); i ++){//現在の配列を取得するif(i == list.indexof(arr)){// array 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アルゴリズムの詳細については、このサイトに興味のある読者は、「Javaデータ構造とアルゴリズムのチュートリアル」、「Java操作DOMノードのヒントの要約」、「Javaファイルの要約およびディレクトリ操作のヒント」、「Java Cache操作のヒントの要約」というトピックを見ることができます。
この記事がみんなのJavaプログラミングに役立つことを願っています。