usaco.section1.2.Palindromic Squares

来源:互联网 发布:格罗玛什地狱咆哮知乎 编辑:程序博客网 时间:2024/05/21 21:39

判断 1 - 300 (十进制)之间,其平方数在 k 进制下为回文串的数,输出其在 k 进制下的结果,和其平方在k 进制下的结果

#include <cstdio>using namespace std;int k;char code[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};int main(){freopen("palsquare.in", "r", stdin);freopen("palsquare.out", "w", stdout);scanf("%d", &k);for(int i = 1; i <= 300; i ++){int s = i * i, tot2 = 0; char b[20];int t = s;while(t){int p = t % k;if(p < 10) b[++ tot2] = (char)p + 48;else b[++ tot2] = code[p - 10];t /= k;}bool ok = 1;for(int j = tot2; j >= 1; j --){if(b[j] != b[tot2 - j + 1]) { ok = 0; break; }}if(!ok) continue;else{char a[20]; int tot = 0; t = i;while(t){int p = t % k;if(p < 10) a[++ tot] = (char)p + 48;else a[++ tot] = code[p - 10];t /= k;}for(int i = tot; i >= 1; i --) printf("%c", a[i]); printf(" ");for(int i = tot2; i >= 1; i --) printf("%c", b[i]); printf("\n");}}return 0;}


0 0
原创粉丝点击