注释掉的输出可以帮助跟踪运行情况。
程序代码:
#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#define MAX 10
int num[MAX];
int readNum()
{
int n,i;
printf("求回文数\n输入要测试的数字个数:");
scanf("%d", &n);
printf("输入%d个要测试的数字(12-100):\n",n);
for(i=0;i<n;i++)
scanf("%d",&num[i]);
// for(i=0;i<n;i++)
// printf("%d ", num[i]);
// printf("\n");
return n;
}
int intrev(int n)
{
char cnum[10];
int result;
itoa(n,cnum,10);
strrev(cnum);
result = atoi(cnum);
//printf("%d -> %d\n",n,result);
return result;
}
int huiwen(int n)
{
char cnum[10],cnum2[10];
int slen,i;
slen = 0;
itoa(n,cnum,10);
itoa(n,cnum2,10);
strrev(cnum2);
slen = strlen(cnum);
for(i=0;i < (slen/2);i++)
{
if(cnum[i] != cnum2[i]) return 0;
}
return 1;
}
int sum(int n, int count)
{
int rev,s;
count++;
rev = intrev(n);
s = n + rev;
//printf("%d + %d = %d, count:%d\n",n,rev,s,count);
if(huiwen(s)) return count;
if(count > 80)
{
count = 0;
return 0;
}
return sum(s,count);
}
int main()
{
int nums,i,count;
nums = readNum();
for (i=0;i<nums;i++)
{
count = 0;
count = sum(num[i],count);
printf("%d\n",count);
}
return 0;
}