java - 使用递归的回文程序

标签 java palindrome

这就是我目前为计算机科学课准备的回文程序。我已经可以正常工作了,除了每当一个单词是回文时,它就是一个无限循环。我知道我必须插入一个数字基本情况,但我不知道该怎么做......我真的很难理解递归。感谢帮助。

public class PalindromeTester
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner (System.in);

        String str, another = "y";
        int left, right;

        while (another.equalsIgnoreCase("y"))
        {

            System.out.println("Enter a potential palindrome:");
            str = scan.next();

            left = 0;
            right = str.length() - 1;

            tester(str, left, right);

            System.out.println();
            System.out.println("Test another palindrome (y/n)?");
            another = scan.next();

        }
    }

    public static void tester (String str, int left, int right)
    {
        Scanner scan = new Scanner (System.in);


        while (str.charAt(left) == str.charAt(right) && left < right)
        {
            System.out.println(str);

            tester( str, left + 1, right -1);

        }
        if (left < right)
        {
            System.out.println("That string is NOT a palindrome.");
        }

        else 
        {

            System.out.println("That string IS a palindrome.");
        }

    }
}

最佳答案

您正在使用 while 循环。通过递归,这是隐式完成的。

您必须将算法分成小部分。

[]代表左,{}代表右。

[1] 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 2 {1} -->Level 0
1 [2] 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 {2} 1 -->Level 1
1 2 [3] 4 5 6 7 8 9 0 9 8 7 6 5 4 {3} 2 1 -->Level 2
1 2 3 [4] 5 6 7 8 9 0 9 8 7 6 5 {4} 3 2 1 -->Level 3
1 2 3 4 [5] 6 7 8 9 0 9 8 7 6 {5} 4 3 2 1 -->Level 4
1 2 3 4 5 [6] 7 8 9 0 9 8 7 {6} 5 4 3 2 1 -->Level 5
1 2 3 4 5 6 [7] 8 9 0 9 8 {7} 6 5 4 3 2 1 -->Level 6
1 2 3 4 5 6 7 [8] 9 0 9 {8} 7 6 5 4 3 2 1 -->Level 7
1 2 3 4 5 6 7 8 [9] 0 {9} 8 7 6 5 4 3 2 1 -->Level 8
1 2 3 4 5 6 7 8 9 {[0]} 9 8 7 6 5 4 3 2 1 -->Level 9

因此,测试人员将继续进行,直到:

  1. 我们已经到达单词的中间了。
  2. 该单词不是回文

案例2示例:

[1] 2 3 A 5 6 7 8 9 0 9 8 7 6 5 4 3 2 {1} 
1 [2] 3 A 5 6 7 8 9 0 9 8 7 6 5 4 3 {2} 1 
1 2 [3] A 5 6 7 8 9 0 9 8 7 6 5 4 {3} 2 1 
1 2 3 [A] 5 6 7 8 9 0 9 8 7 6 5 {4} 3 2 1 --> !!!

我认为这种方法对于理解递归是如何工作的非常有帮助

public static String positions(String word, int l, int r) {
        char[] a = word.toCharArray();
        String s = "";
        // [letter] if left, {} if right, [{}] if both 
        for (int i = 0; i < a.length; i++) {
            if (l == i && r == i) {
                s += "{[" + a[i] + "]}";
            } else if (l == i) {
                s += "[" + a[i] + "]";
            } else if (r == i) {
                s += "{" + a[i] + "}";
            } else {
                s += a[i];
            }
            s+=" ";
        }
        return s;

    }

最后是tester方法。

public static boolean tester(String str, int left, int right) {

        System.out.println(positions(str, left, right) +" tester(str, "+left +", "+right+")");
        if (left>=right) // case 1
            return true; // that's ok, we've reached the middle
            // the middle was not reached yet.
            // is the condition satisfied?
        if (str.charAt(left) == str.charAt(right)) {
            // yes. So, lets do it again, with the parameters changed
            return tester(str, left + 1, right - 1);

        } 
        //the condition was not satisfied. Let's get out of here.
        else {

            return false;
        }

    }

一些输出:

Enter a potential palindrome:
1234567890987654321
[1] 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 2 {1}  tester(str, 0, 18)
1 [2] 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 {2} 1  tester(str, 1, 17)
1 2 [3] 4 5 6 7 8 9 0 9 8 7 6 5 4 {3} 2 1  tester(str, 2, 16)
1 2 3 [4] 5 6 7 8 9 0 9 8 7 6 5 {4} 3 2 1  tester(str, 3, 15)
1 2 3 4 [5] 6 7 8 9 0 9 8 7 6 {5} 4 3 2 1  tester(str, 4, 14)
1 2 3 4 5 [6] 7 8 9 0 9 8 7 {6} 5 4 3 2 1  tester(str, 5, 13)
1 2 3 4 5 6 [7] 8 9 0 9 8 {7} 6 5 4 3 2 1  tester(str, 6, 12)
1 2 3 4 5 6 7 [8] 9 0 9 {8} 7 6 5 4 3 2 1  tester(str, 7, 11)
1 2 3 4 5 6 7 8 [9] 0 {9} 8 7 6 5 4 3 2 1  tester(str, 8, 10)
1 2 3 4 5 6 7 8 9 {[0]} 9 8 7 6 5 4 3 2 1  tester(str, 9, 9)
true

Test another palindrome (y/n)?
y
Enter a potential palindrome:
12345A678654321
[1] 2 3 4 5 A 6 7 8 6 5 4 3 2 {1}  tester(str, 0, 14)
1 [2] 3 4 5 A 6 7 8 6 5 4 3 {2} 1  tester(str, 1, 13)
1 2 [3] 4 5 A 6 7 8 6 5 4 {3} 2 1  tester(str, 2, 12)
1 2 3 [4] 5 A 6 7 8 6 5 {4} 3 2 1  tester(str, 3, 11)
1 2 3 4 [5] A 6 7 8 6 {5} 4 3 2 1  tester(str, 4, 10)
1 2 3 4 5 [A] 6 7 8 {6} 5 4 3 2 1  tester(str, 5, 9)
false

Test another palindrome (y/n)?

main方法中,

System.out.println(tester(str, left, right));

为了查看true/false输出

关于java - 使用递归的回文程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22896285/

相关文章:

java - 应该如何将 tomcat 与现有项目一起使用?

java - 难以理解 <? Java 中扩展 T> 通配符

java - 如何在运行时更新 Java 应用程序的库?

c++ - 我如何以这种方式退出字符串数组

python - 在python中生成所有3位回文数的列表

java - 从数据库 SQL 查询创建 Java 对象

java - 使用 charAt() 而不使用compareTo() 方法按字典顺序比较两个字符串

python - 检查给定的输入词是否为回文

Java回文程序无法运行

c - 查找间隔中回文数的算法