#include <stdio.h>
#include <string.h>
char a[10000000];
char b[10000000];
char c[10000000];
void reverse(char *s)
{
char temp;
char *last = s;
while (*last != '\0')
{
last++;
}
last--;
while (s<last)
{
temp = *s;
*s = *last;
*last = temp;
s++;
last--;
}
}
int isreverse(const char *s)
{
int i;
int len = strlen(s)-1;
for (i=0; i<len; i++,len--)
{
if (s[i] != s[len])
{
return 0;
}
}
return 1;
}
int main()
{
int i;
while (scanf("%s",a) != EOF)
{
while (!isreverse(a))
{
int len = strlen(a)-1;
int carry = 0;
strcpy(b,a);
reverse(b);
printf("%s--->",a);
for (i=0; i<=len;i++)
{
c[i] = (a[i]-'0' + b[i] - '0' + carry);
carry = (c[i]) / 10;
c[i] = c[i] % 10 + '0';
}
if (carry != 0)
{
c[i] = carry + '0';
c[++i] = '\0';
}
else
{
c[i] = '\0';
}
reverse(c);
strcpy(a,c);
}
printf("%s\n",a);
}
return 0;
}
每日一算法:回文数
最新推荐文章于 2019-08-04 19:13:13 发布