您的位置:知识库 » 求职面试

用递归实现回文判断

作者: cnyao  来源: 博客园  发布时间: 2009-11-26 21:44  阅读: 7478 次  推荐: 1   原文链接   [收藏]  

回文,12321

这是一个字符串,字符串的第一个和最后一个相同,第二个和倒数第二个相同,这样来实现回文判断。

 1 #include <iostream>
 2 using namespace std;
 3 
 4 bool isPalindrome(char str[], size_t n)
 5 {
 6     if (n<=1)
 7     {
 8         return true;
 9     }
10     else if (str[0]==str[n-1])
11     {
12         return isPalindrome(str+1,n-2);
13     }
14     else
15         return false;
16 }
17 
18 int main()
19 {
20     char str[20];
21     cin>>str;
22     cout<<(isPalindrome(str,strlen(str))?"Yes":"No")<<endl;
23 }
1
0
标签:面试题集

求职面试热门文章

    求职面试最新文章

      最新新闻

        热门新闻