萌胖子 2015-09-24 14:42 采纳率: 0%
浏览 1458

java递归方法求解:2BCD+BCDE=DA01,将字母替换成不同的数字满足等式

如题

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

怎么用递归求解

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-09-24 15:37
    关注

    递归求解的关键是你要修改 isSolved,使得它有识别部分数字等式是否成立的能力。
    当然,为了递归而递归,无非就是循环数字替换掉一个字符,然后让递归的下一层再替换掉一个,直到完全替换掉为止。
    总之这个题目用递归没有什么特别的意义。不是非用不可

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题