現在、再帰は私にとって新鮮で難しいトピックですが、アルゴリズムの1つで使用する必要があります。
課題は次のとおりです。
再帰の数 (ネストされた FOR ループの数) と各 FOR ループの反復数を指定する方法が必要です。結果はカウンターに似たものを表示するはずですが、カウンターの各列は特定の数に制限されています。
ArrayList<Integer> specs= new ArrayList<Integer>();
specs.add(5); //for(int i=0 to 5; i++)
specs.add(7);
specs.add(9);
specs.add(2);
specs.add(8);
specs.add(9);
public void recursion(ArrayList<Integer> specs){
//number of nested loops will be equal to: specs.size();
//each item in specs, specifies the For loop max count e.g:
//First outside loop will be: for(int i=0; i< specs.get(0); i++)
//Second loop inside will be: for(int i=0; i< specs.get(1); i++)
//...
}
結果は、この手動のネストされたループの出力に似ています。
int[] i;
i = new int[7];
for( i[6]=0; i[6]<5; i[6]++){
for( i[5]=0; i[5]<7; i[5]++){
for(i[4] =0; i[4]<9; i[4]++){
for(i[3] =0; i[3]<2; i[3]++){
for(i[2] =0; i[2]<8; i[2]++){
for(i[1] =0; i[1]<9; i[1]++){
//...
System.out.println(i[1]+" "+i[2]+" "+i[3]+" "+i[4]+" "+i[5]+" "+i[6]);
}
}
}
}
}
}
私はすでにこれで3日間殺しましたが、まだ結果はありません。インターネットで検索していましたが、例が違いすぎます。したがって、プログラミングの質問をインターネットに投稿するのは人生で初めてです。前もって感謝します。コードの効率を自由に変更できます。同じ結果が必要なだけです。