我正在研究一個項目,因爲我們正在開始研究遞歸。我對此很新,所以我不知道如何解決這個難題。這是本書中的問題,我會很感激任何STEPS(step1,step2,...),這可能會幫助我想出辦法來解決這個問題。另外,如果有人能夠與我分享一種方法,我真的很感激理解遞歸,但它對我來說並沒有什麼意義,所以我會很感激關於如何更好地理解這個話題的任何建議。好的,這些是說明。謝謝。java中的遞歸Additionpuzzle
加法拼圖的形式如2BCD + BCDE = DA01。我們想找到所有的解決方案,其中A,B,C,D是不同的數字,不同於謎題中的任何數字。這裏,一個解決方案是2345 + 3456 = 5801。一般來說,一個謎題可以有多達十個數字和數字的任意組合。寫一個遞歸方法使用這兩類計算解決一個難題:
級#1
public class ThisPuzzle
{
/**
Returns a solution to a puzzle.
@param p a puzzle
@return a solution or null if none exists
*/
public static Puzzle solvePuzzle(Puzzle p)
{
// ...
return null;
}
public static void main(String[] args)
{
Puzzle p = new Puzzle("3A6", "36B", "71C");
System.out.println(solvePuzzle(p));
}
}
類#2
public class Puzzle
{
private String add1;
private String add2;
private String result;
/**
Constructs a puzzle.
@param add1 a string containing digits 0 - 9 and letters
@param add2 a string containing digits 0 - 9 and letters
@param result a string containing digits 0 - 9 and letters
*/
public Puzzle(String add1, String add2, String result)
{
this.add1 = add1;
this.add2 = add2;
this.result = result;
}
/**
Makes a new puzzle by replacing a letter with a digit.
@param letter the letter to be replaced
@param digit the digit to replace it with
@return the new puzzle
*/
public Puzzle replace(String letter, int digit)
{
// ...
}
/**
Returns true if the puzzle is solved.
@return true if the puzzle has no letters and the
first two numbers add up to the third
*/
public boolean isSolved()
{
// ...
}
/**
Gets the first letter in this puzzle.
@return the first letter, or "" if there are no letters.
*/
public String firstLetter()
{
// ...
}
/**
Checks whether this puzzle contains a given digit.
@param digit a digit
@return true if this puzzle returns digit
*/
public boolean contains(int digit)
{
// ...
}
public String toString()
{
return add1 + "+" + add2 + "=" + result;
}
}