递归算法:
//
// author: kangquan@scut2008
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int is_huiwen(char * num, int begin, int end)
{
if(begin >= end)
return 1;
if(num[begin] == num[end])
{
is_huiwen(num,begin+1,end-1);
}
else{
return -1;
}
}
int main()
{
char * num;
printf("input a num:\n");
scanf("%s:",num);
printf("%s",is_huiwen(num,0,strlen(num)-1) == 1 ? "It's." : "It's not." );
return 0;
}
非递归算法:
/
//
// author: kangquan@scut2008
//
// blog:http://blog.csdn.net/kangquan2008
//
// description: creats a small heap,
// and sorts the datas from small to big.
//
/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// if reverse the num,and it still is the same with the old,
// it's a 'huiwen'(sorry for my poor english) data.
bool is_huiwen(int num)
{
int reverse = 0;
for(int tmp=num; tmp!=0; tmp /= 10)
{
reverse = reverse * 10 + tmp % 10;
}
if(reverse == num)
return true;
else
return false;
}
int main()
{
int num;
printf("Please input a num\n");
scanf("%d",&num);
printf("%s\n",is_huiwen(num)?"It's.":"It's not.");
return 0;
}