AcWing 1346. 回文平方
原题链接
简单
作者:
玖玖
,
2021-01-13 11:54:49
,
所有人可见
,
阅读 314
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int B;
string f(int x)//将十进制转化为B进制
{
string t = "";
while(x)
{
int k = x % B;
if(k < 10)
{
t += (k + '0');
}
else t += ((k - 10) + 'A');
x /= B;
}
reverse(t.begin(), t.end());
return t;
}
bool check(string t)//检查t这个数是否是回文数
{
for(int i = 0, j = t.size() - 1; i <= j; i++, j--)
{
if(t[i] != t[j]) return false;
}
return true;
}
int main(void)
{
cin >> B;
for(int i = 1; i <= 300; i++)
{
if(check(f(i * i)))//检查i*i转化为B进制后是否为回文数
{
cout << f(i) << ' ' << f(i * i) << endl;
}
}
return 0;
}