最长回文数的位数+C语言
时间: 2023-12-19 18:06:11 浏览: 109
c语言面试题之哈希表最长回文串.zip
以下是求解最长回文数位数的C语言代码:
```c
#include <stdio.h>
#include <string.h>
#define MAXN 1010
char s[MAXN];
int dp[MAXN][MAXN];
int main() {
fgets(s, MAXN, stdin);
int len = strlen(s);
if (s[len - 1] == '\n') s[--len] = '\0'; // 去掉fgets读入的换行符
memset(dp, 0, sizeof(dp));
for (int i = 0; i < len; i++) {
dp[i][i] = 1;
if (i < len - 1) {
if (s[i] == s[i + 1]) dp[i][i + 1] = 1;
}
}
for (int L = 3; L <= len; L++) {
for (int i = 0; i + L - 1 < len; i++) {
int j = i + L - 1;
if (s[i] == s[j] && dp[i + 1][j - 1] == 1) dp[i][j] = 1;
}
}
int ans = 0;
for (int i = 0; i < len; i++) {
for (int j = i; j < len; j++) {
if (dp[i][j] == 1 && j - i + 1 > ans) ans = j - i + 1;
}
}
printf("%d\n", ans);
return 0;
}
```
阅读全文