我发现我越简单的题越容易出小错误= =希望考试的时候不要o(>﹏<)o!这道题样例都没有给对称中心不是字母的情况啊……想了半天,所以主函数一大坨。
#include <stdio.h>
#include <string.h>
int main()
{
char s[1001];
int i,j, longest = 1,len,length;
gets_s(s);
len = strlen(s);
for (i = 0; s[i]!='\0'; i++) {/*到i时能找到的最大对称长度为2*max(len-i-1,i)+1*/
if (2 * (len - i - 1) + 1 <= longest && 2 * i + 1 <= longest)
continue;
length = 1;
for (j = 1; j <= len - i - 1 && j <= i; j++)
if (s[i + j] != s[i - j])
break;
else length+=2;
if (length > longest)
longest = length;
}
for (i = 0; s[i] != '\0'; i++) {
if (s[i] != s[i + 1])
continue;
length = 2;
for (j = 1; j <= len - i - 2 && j<= i; j++)
if (s[i + j + 1] != s[i - j])
break;
else length += 2;
if (length > longest)
longest = length;
}
printf("%d",longest);
return 0;
}