标签:
/*ID: awsd1231PROG: palsquareLANG: C++*/#include<iostream>#include<cstdio>#include<cmath>#include<cstring>using namespace std;const char c[10] = {‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘G‘,‘H‘,‘I‘,‘J‘};int B;char xB[20] = {0}, xbb[20] = {0};void toB(int xq) {int idx = 0;bool begin = true;for(int i = 20; i != -1; --i) {if(begin && pow(B, i) > xq) continue;//排除开头0else{begin = false;for(int j = B-1; j != -1; --j) {if(j * pow(B, i) <= xq) {xq -= j * pow(B, i);if(j < 10) xbb[idx++] = j + ‘0‘;else xbb[idx++] = c[j - 10];break;}}}}printf("%s ", xbb);}bool isPalindromic(int xq) {//输出x 和 B进制的x平方int idx = 0;bool begin = true;for(int i = 20; i != -1; --i) {if(begin && pow(B, i) > xq) continue;//排除开头0else{begin = false;for(int j = B-1; j != -1; --j) {if(j * pow(B, i) <= xq) {xq -= j * pow(B, i);if(j < 10)xB[idx++] = j + ‘0‘;else xB[idx++] = c[j - 10];break;}}}}int len = strlen(xB), t;for(int i = 0; i != len/2; ++i) {if(xB[i] != xB[len - i -1])return false;}return true;}int main() {freopen("palsquare.in", "r", stdin);freopen("palsquare.out", "w", stdout);cin >> B;for(int i = 1; i != 301; ++i)if(isPalindromic(i*i)) {toB(i);printf("%s\n", xB);}return 0;}
标签:
原文地址:http://www.cnblogs.com/liangyongrui/p/4541857.html