[小算法] 递归实现回文判断
2011-08-25 16:39 Kevin Pan 阅读(317) 评论(0) 编辑 收藏 举报回文形式:ABCDEFEDCBA, 就是顺着念和倒着念是一样的。
代码如下:
#include "stdafx.h"
#include <iostream>
using namespace std;
bool PalindromeCheck(char * str, int n);
int main()
{
char * str = "abcdefedcba";
bool result = PalindromeCheck(str, strlen(str));
cout << "string: " << str << ", result: " << (result ? "Yes" : "No") << endl;
system("pause");
}
bool PalindromeCheck(char * str, int n)
{
if(n > 0)
{
if(str[0] == str[n - 1])
{
return PalindromeCheck(str + 1, n - 2);
}
else
{
return false;
}
}
else
{
return true;
}
}
#include <iostream>
using namespace std;
bool PalindromeCheck(char * str, int n);
int main()
{
char * str = "abcdefedcba";
bool result = PalindromeCheck(str, strlen(str));
cout << "string: " << str << ", result: " << (result ? "Yes" : "No") << endl;
system("pause");
}
bool PalindromeCheck(char * str, int n)
{
if(n > 0)
{
if(str[0] == str[n - 1])
{
return PalindromeCheck(str + 1, n - 2);
}
else
{
return false;
}
}
else
{
return true;
}
}