USACO section1.2 Dual Palindromes
给出 n 和 s (十进制),打印 s 后面 n 个在 2-10 进制中至少两个进制下为回文数的十进制表示。
/* PROG: dualpal LANG: C++ */ # include <cstdio> # include <cstring> int n, s; void strRev(char *s) { char ch; int len = strlen(s), mid = len / 2; for (int i = 0; i < mid; ++i) ch = s[i], s[i] = s[len-1-i], s[len-1-i] = ch; } void to(int base, int x, char *s) { int i = 0; while (x > 0) { s[i++] = x % base + '0'; x /= base; } s[i] = '\0'; strRev(s); } char isPal(char *s) { int len = strlen(s), mid = len / 2; for (int i = 0; i < mid; ++i) if (s[i] != s[len-1-i]) return 0; return 1; } int main() { freopen("dualpal.in", "r", stdin); freopen("dualpal.out", "w", stdout); scanf("%d%d", &n, &s); int cnt = 0; char buf[30]; for (int i = s+1; cnt < n; ++i) { char ok = 0; int c = 0; for (int j = 2; j <= 10; ++j) { to(j, i, buf); if (isPal(buf)) ++c; if (c >= 2) {ok = 1; break;} } if (ok) {printf("%d\n", i);++cnt;} } fclose(stdin); fclose(stdout); return 0; }
/**/