簡體   English   中英

使用遞歸的回文計划

[英]Palindrome Program using Recursion

目前,這是我的計算機科學課“回文”計划的內容。 我有很多工作,除了一個詞是回文,這是一個無限循環。 我知道我必須插入一個數字基數,但是我不知道該怎么做...我真的很難理解遞歸。 感謝幫助。

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

因此, tester將繼續進行以下操作:

  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輸出

由於您正在使用遞歸(其基本用途主要是用於消除循環),因此tester()方法內的while循環if應該為if

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

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

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

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

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

我修改了您的tester()方法, whileif替換了您的while並移動了第二個if子句。

public static void tester(String str, int left, int right) {
        if (str.charAt(left) == str.charAt(right) && left < right) {
            tester(str, left + 1, right - 1);
        } else {
            if (left < right) {
                System.out.println("That string is NOT a palindrome.");
            } else {
                System.out.println("That string IS a palindrome.");
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM