给出一个数 n(十进制),求出 1-300 范围内所有平方(n 进制)为回文串的数,并打印。
/* PROG: palsquare LANG: C++ */ # include <cstdio> # include <cstring> # define N 300 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, t; while (x > 0) { t = x % base; if (t > 9) t += 'A'-10; else t += '0'; s[i++] = t; 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-i-1]) return 0; return 1; } int main() { int n; freopen("palsquare.in", "r", stdin); freopen("palsquare.out", "w", stdout); scanf("%d", &n); for (int i = 1; i <= N; ++i) { char a[10], b[20]; to(n, i, a), to(n, i*i, b); if (isPal(b)) printf("%s %s\n", a, b); } fclose(stdin); fclose(stdout); return 0; }
/**/