0

現在、再帰は私にとって新鮮で難しいトピックですが、アルゴリズムの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日間殺しましたが、まだ結果はありません。インターネットで検索していましたが、例が違いすぎます。したがって、プログラミングの質問をインターネットに投稿するのは人生で初めてです。前もって感謝します。コードの効率を自由に変更できます。同じ結果が必要なだけです。

4

4 に答える 4

1
// ...
   recursion (specs, specs.size () - 1);    

// ...

   public void recursion(ArrayList<Integer> specs, int startWith){

      for (int i = 0; i < specs.get(startWith); i++) {
         // ...
         if (startWith - 1 >= 0)
            recursion (specs, startWith - 1);
      }
    }
于 2012-06-03T10:22:01.207 に答える
1

関数にはspecs、反復に使用する配列のインデックスと、出力する必要がある以前の数値も必要です。

public void recursion(ArrayList<Integer> specs, int index, String output) {
    if( index >= specs.size() ) {
         System.out.println(output);
        return;
    }
    for (int i = 0; i < specs.get(index); i++ )
        recursion( specs, index+1, Integer.toString(i) + " " + output );
}

次のように呼び出す必要があります。

ArrayList<Integer> specs= new ArrayList<Integer>();
specs.add(5);
specs.add(7);
specs.add(9);
specs.add(2);
specs.add(8);
specs.add(9);

recursion( specs, 0, "" );
于 2012-06-03T10:35:33.553 に答える
0

このスニペットは、必要な出力を提供しますか? (コンパイル可能で実行可能です)

import java.util.ArrayList;
import java.util.List;

public class SO {

    static ArrayList<Integer> specs = new ArrayList<Integer>();
    static int[] i;

    public static void main(String[] args) throws Exception {
        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);
        i = new int[specs.size()];
        printMe(0, specs, i);
    }

    static void printMe(int depth, List<Integer> _specs, int[] i) {
        if (_specs.isEmpty()) {
            System.out.println(printI(i));
            return;
        } else {
            for (int j = 0; j < _specs.get(0); j++) {
                i[depth] = j + 1; // + 1 since you seems to want to go from 1 and not 0
                printMe(depth + 1, _specs.subList(1, _specs.size()), i);
            }
        }
    }

    static String printI(int[] i) {
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < i.length; j++) {
            sb.append(i[j]);
            if (j < i.length - 1) {
                sb.append(" ");
            }

        }
        return sb.toString();
    }
}
于 2012-06-03T10:37:01.267 に答える
0

これを試すことができます:

public static void loops(ArrayList<Integer> specs, int idx, StringBuilder res){
    if(idx==specs.size()-1){
        for (int i = 0; i < specs.get(idx); i++) {
            System.out.println(i+" "+res);
        }
    }
    else{
        for(int i=0;i<specs.get(idx);i++){
            res.insert(0,i+" ");
            loops(specs,idx+1,res);
            res.delete(0, 2);
        }
    }
}

そして次のように呼び出します:

    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);
    loops(specs,0, new StringBuilder());
于 2012-06-03T10:47:18.423 に答える