/*
TASK:palsquare
LANG:C++
ID:huibaochen
*/
/*一题:给你一个数n代表进制,算出从1到300的数的平方转化为n进制后是否进回文序列,是的话输出这个是的n进制和这个数的平方的n进制*/
#include <iostream>
#include <stdio.h>
#include <string.h>
int m, n, len, num, r;
char str[1000 +5], ch[100 + 5];
using namespace std;
void yes(int m)
{
num = m * m;
len = 0;
while(num){
r = num % n;
num = num / n;
if(r >= 0 && r <= 9)
str[len++] = r + 48;
else
str[len++] = r + 'A' - 10;
}
for(int i = 0; i < len / 2; i++){
if(str[i] != str[len - 1 - i])
return;
}
len = 0;
while(m){
r = m % n;
m = m / n;
if(r >= 0 && r <= 9)
ch[len++] = r + 48;
else
ch[len++] = r + 'A' - 10;
}
for(int i = 0; i < len / 2; i++){
swap(ch[i], ch[len - i - 1]);
}
printf("%s %s\n", ch, str);
}
int main()
{
freopen ("palsquare.in", "r", stdin);
freopen ("palsquare.out", "w", stdout);
scanf("%d", &n);
for(int i = 1; i <= 300; i++){
yes(i);
}
return 0;
}
USACO:Palindromic Squares;回文判断+进制转化
最新推荐文章于 2024-05-25 09:55:59 发布