如题
public class Puzzle {
private String add1;
private String add2;
private String result;
private Puzzle nowPuzzle;
/**
* 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;
}
public Puzzle getNowPuzzle() {
return nowPuzzle;
}
/**
* 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) {
String nadd1 = this.add1.replace(letter, digit + "");
String nadd2 = this.add2.replace(letter, digit + "");
String nresult = this.result.replace(letter, digit + "");
nowPuzzle = new Puzzle(nadd1, nadd2, nresult);
return nowPuzzle;
}
/**
* Gets the first letter in this puzzle.
*
* @return the first letter, or "" if there are no letters.
*/
public String firstLetter() {
String temp = this.add1 + this.add2 + this.result;
char[] str = temp.toCharArray();
for (char c : str) {
if (c >= 65 && c <= 90) {
return c + "";
}
}
return "";
}
/**
* Returns true if the puzzle is solved.
*
* @return true if the puzzle has no letters, no number starts with zero,
* and the first two numbers add up to the third
*/
public boolean isSolved() {
if (this.firstLetter().equals("") && !add1.startsWith("0") && !add2.startsWith("0") && !result.startsWith("0")) {
if (Integer.parseInt(add1) + Integer.parseInt(add2) == Integer.parseInt(result)) {
return true;
}
}
return false;
}
/**
* Checks whether this puzzle contains a given digit.
*
* @param digit a digit
* @return true if this puzzle returns digit
*/
public boolean contains(int digit) {
if (this.add1.contains(digit + "") || this.add2.contains(digit + "") || this.result.contains(digit + "")) {
return true;
}
return false;
}
public String toString() {
return add1 + "+" + add2 + "=" + result;
}
}
怎么用递归求解