We talked about some recursive ideas and some conceptual understandings. Here we try to solve some problems with recursion. For example, text.
A text return is a string that is the same as reading and reading. For example, level and eye are all input files. The iterative method can be used to quickly determine whether a string is a return object. How can we implement it using recursive methods?
First, we need to consider two recursive conditions: Macau casino gameplay
- First, can this problem be resolved into the same form but smaller scale?
- Second: if such a decomposition exists, is there a simple situation for such decomposition?
First, let's see if there is a qualified decomposition at the first point. It is easy to find that if a string is a return, there must be a smaller return within it. For example, the eve in level is also a text return. Furthermore, we note that the first character of a bounce message must be the same as the last character.
Therefore, we naturally have the following method:
First, judge whether the first and last characters of the given string are equal. If they are equal, then determine whether the string after the first and last characters are removed as a background. If they are not equal, the string is not a background.
Note: We have successfully reduced the problem. The string with the first and last characters removed is certainly smaller than the original string.
Next, let's look at the second point. Is there a simple situation for such decomposition? Simple situations are required when recursion is used. Otherwise, your recursive program may enter endless calls.
For the background problem, we can easily find that a single character string must be a background, so only one character is a simple situation, but it is not the only simple situation, because the empty string is also a return. In this way, we can see two simple situations of the Back-to-text problem: the number of characters is 1 and the number of characters is 0.
Well, both conditions are met. Based on the above analysis, we can easily compile a recursive implementation method to solve the problem of returning the text:
09 |
printf ( "Enter the string to be retrieved :" ); |
13 |
rs = is_palindereme(str, n); |
17 |
int is_palindereme( char *str, int n) |
19 |
printf ( "Length: %d \n" ,n); |
20 |
printf ( "%c ----- %c\n" , str[0], str[n-1]); |
24 |
//printf("%d, %d\n", str[0], str[n-1]); |
25 |
return ((str[0] == str[n-1]) ? is_palindereme(str+1, n-2) : 0); |
The program running result is:
View Source print?
1 |
Enter the level string to judge the background. |
Recursive Determination of string retrieval Phenomena