簡體   English   中英

Java:遞歸循環的組合,里面有不同的FOR循環; 輸出:FOR循環索引

[英]Java: Combination of recursive loops which has different FOR loop inside; Output: FOR loops indexes

目前遞歸對我來說是新鮮和困難的話題,但是我需要在我的算法中使用它。

這是挑戰:

我需要一個方法,我指定遞歸的數量(嵌套的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天,但仍然沒有結果,在網上搜索它,但是例子太不一樣了。 因此,在我生命中第一次在互聯網上發布編程問題。 提前謝謝,您可以自由更改代碼效率,我只需要相同的結果。

// ...
   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);
      }
    }

您的函數現在還需要用於迭代的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, "" );

此代碼段是否提供您想要的輸出? (它是可編譯的和可執行的)

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();
    }
}

你可以試試這個:

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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM