我不明白这个递归程序

时间:2013-11-13 00:06:58

标签: java recursion

我有这个代码使用递归来获取输入的任何字符串的每个可能的字符组合。但我不明白程序运行时会发生什么!有人可以解释一下这个程序会发生什么吗?我对编程还很陌生,所以如果您的解释不太复杂,我将不胜感激,谢谢!

public class WordJumble {
  public static void main(String[] args) {
    String letters = "WORD";
    jumbleWords(letters, "");
  }

  //input parameters
  //word - the remaining letters in the word still to jumble
  //jumbLet - the letters already used to create the jumbled word

  public static void jumbleWords(String word, String jumbLet) {
    int pos;
    String remainingLetters;
    String origWord = word;
    String origJumbledLetters = jumbLet;
    if (word.length() == 1) 
      System.out.println(jumbLet + word);
    else {
      for (pos = 0; pos < origWord.length(); pos++) {
        remainingLetters = origWord.substring(0, pos) + origWord.substring(pos + 1, origWord.length());
        //recursive call to jumbleWords()
        jumbleWords(remainingLetters, origJumbledLetters + origWord.charAt(pos));
      }
    }
  }
}

然后输出:

WORD
WODR
WROD
WRDO
WDOR
WDRO
OWRD
OWDR
ORWD
ORDW
ODWR
ODRW
RWOD
RWDO
ROWD
RODW
RDWO
RDOW
DWOR
DWRO
DOWR
DORW
DRWO
DROW

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

这个递归算法正在做什么:接收初始字符串“WORD”

然后一次移动一个字符,移动该字符后,它会跟踪哪些子字符串已创建,以及哪些字符尚未移动。它将它传回自身以重新混淆这个词。

pos + 1正在将角色项移动到字符串的下一个位置

答案 1 :(得分:0)

如果打印出递归调用并添加一个记忆递归深度的附加参数(并转换为制表符),您可以完美地观察到会发生什么:

public class WordJumble {
  public static void main(String[] args) {
    String letters = "WORD";
    jumbleWords(letters, "", 0);
  }

  //input parameters
  //word - the remaining letters in the word still to jumble
  //jumbLet - the letters already used to create the jumbled word

  public static void jumbleWords(String word, String jumbLet, int recursionDepth) {
    int pos;
    String remainingLetters;
    String origWord = word;
    String origJumbledLetters = jumbLet;

    String tabs = "";
    if(recursionDepth > 0) { // translate recursionDepth to tab-characters
        tabs = String.format("%0" + recursionDepth + "d", 0).replace("0","\t");
    }

    if (word.length() == 1) 
      System.out.println(tabs + jumbLet + " + " + word);
    else {
      for (pos = 0; pos < origWord.length(); pos++) {
        remainingLetters = origWord.substring(0, pos) + origWord.substring(pos + 1, origWord.length());
        //recursive call to jumbleWords()
        System.out.println(tabs + "jumbleWords("+remainingLetters+", "+origJumbledLetters + " + " + origWord.charAt(pos) + ");");
        jumbleWords(remainingLetters, origJumbledLetters + origWord.charAt(pos), recursionDepth + 1);
      }
    }
  }
}

输出结果为:

jumbleWords(ORD,  + W);
    jumbleWords(RD, W + O);
        jumbleWords(D, WO + R);
            WOR + D
        jumbleWords(R, WO + D);
            WOD + R
    jumbleWords(OD, W + R);
        jumbleWords(D, WR + O);
            WRO + D
        jumbleWords(O, WR + D);
            WRD + O
    jumbleWords(OR, W + D);
        jumbleWords(R, WD + O);
            WDO + R
        jumbleWords(O, WD + R);
            WDR + O
jumbleWords(WRD,  + O);
    jumbleWords(RD, O + W);
        jumbleWords(D, OW + R);
            OWR + D
        jumbleWords(R, OW + D);
            OWD + R
    jumbleWords(WD, O + R);
        jumbleWords(D, OR + W);
            ORW + D
        jumbleWords(W, OR + D);
            ORD + W
    jumbleWords(WR, O + D);
        jumbleWords(R, OD + W);
            ODW + R
        jumbleWords(W, OD + R);
            ODR + W
jumbleWords(WOD,  + R);
    jumbleWords(OD, R + W);
        jumbleWords(D, RW + O);
            RWO + D
        jumbleWords(O, RW + D);
            RWD + O
    jumbleWords(WD, R + O);
        jumbleWords(D, RO + W);
            ROW + D
        jumbleWords(W, RO + D);
            ROD + W
    jumbleWords(WO, R + D);
        jumbleWords(O, RD + W);
            RDW + O
        jumbleWords(W, RD + O);
            RDO + W
jumbleWords(WOR,  + D);
    jumbleWords(OR, D + W);
        jumbleWords(R, DW + O);
            DWO + R
        jumbleWords(O, DW + R);
            DWR + O
    jumbleWords(WR, D + O);
        jumbleWords(R, DO + W);
            DOW + R
        jumbleWords(W, DO + R);
            DOR + W
    jumbleWords(WO, D + R);
        jumbleWords(O, DR + W);
            DRW + O
        jumbleWords(W, DR + O);
            DRO + W