思路:这道题,我用了两个函数,第一个函数判断是否是回文数,第二个函数里面将传入的s进行反转(注意,PTA上的编译器不支持C中的反转函数strver()),再进行相加,再进行输出,最后返回相加后的结果,然后利用strcpy()函数,将相加后的结果重新赋值给s
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int ishuiwen(char s[])
{
int len = strlen(s)-1;
for (int i = 0; i <= len ; i++)
if (s[i] != s[len - i]) return 0;
return 1;
}
char* zhuan(char s[])
{
char s1[10000], s2[10000] , s3[10000];
int k = 0,len=strlen(s),tmp=0,n=0,k1=0;
for (int i = len - 1; i >= 0; i--)
s1[k++] = s[i];
s1[k] = '\0';
for (int i = 0; i < len; i++)
{
int t = (s[i] - '0') + (s1[i] - '0')+n;
s2[tmp++] = t % 10+'0';
n = t / 10;
}
if (n > 0) s2[tmp++] = n + '0';
s2[tmp] = '\0';
for (int i = tmp - 1; i >= 0; i--)
s3[k1++] = s2[i];
s3[k1] = '\0';
printf("%s + %s = %s\n", s, s1,s3);
return s3;
}
int main()
{
char s[10000];
scanf("%s", s);
if (ishuiwen(s)==1) printf("%s is a palindromic number.\n", s);
else {
int cot = 0;
while (cot<10)
{
strcpy(s, zhuan(s));
if (ishuiwen(s)==1)
{
printf("%s is a palindromic number.\n", s);
break;
}
cot++;
}
if (cot == 10) printf("Not found in 10 iterations.\n");
}
system("pause");
return 0;
}