Analyzing palindrome
I, entitled: string determines whether the input palindromic series.
Second, the design ideas:
1, an initial determination if the length of the string is 0 or 1, it must be returned directly palindromic exit.
2, write termination condition, if the last remaining one is a palindrome Back to exit. If it is two to determine whether They are equal if f is equal to 1 or f 0, returns an exit.
3, write normal circumstances, if the ending is equal to n plus 1, calls itself, if not equal before and after the middle of f 0, returns an exit.
Third, the source code:
import java.util.Scanner; public class Huiwen { static char []a=new char[1000]; static String str; static int f=0; static int n=0; static Scanner sc=new Scanner(System.in); public static void main(String[] args) { str=sc.nextLine(); a=str.toCharArray(); huiWen(a,n); if(f==0) { System.out.println (STR + ":" + "is not a palindrome" ); } the else { System.out.println (STR + ":" + "palindromic sequence" ); } } public static void Huiwen ( char [] A, int n-) { IF (a.length a.length == == 0 ||. 1 ) { F =. 1 ; return ; } IF (a.length-2-n-* ==. 1. 1 ) { F =. 1 ; return ; } IF(a.length-2*n-1==2) { if(a[n]==a[a.length-n-1]) { f=1; return ; } else { f=0; return ; } } if(a[n]==a[a.length-n-1]) { n=n+1; huiWen(a,n); } else { f=0; return ; } } }
Fourth, the test results
V. Conclusion:
Recursive three elements:
1, an unequivocal end to conditions, given that the end of the recursive approach.
2, the result of extraction problem, reduce the size of the problem.
3, calls itself.