AcWing 1346. 回文平方
原题链接
简单
作者:
acw_yxy
,
2021-01-14 12:29:41
,
所有人可见
,
阅读 226
算法1
(进制转换)
C++ 代码
// 进制转化
#include <iostream>
#include <algorithm>
using namespace std;
int B;
// 数字转换进制
string turnToB(int A)
{
vector<int> ans;
// 相除转换进制法
while(A)
{
ans.push_back(A % B);
A /= B;
}
string res;
// 字符串直接加数字,强制转换了
for(auto x : ans)
if(x <= 9) res += x + '0';
else res += x - 10 + 'A';
reverse(res.begin(), res.end());
return res;
}
// 双指针检查
bool check(string s)
{
for(int i = 0, j = s.size() - 1; i <= j; i ++, j--)
{
if(s[i] != s[j]) return false;
}
return true;
}
int main()
{
cin >> B;
for(int i = 1; i < 300; i ++)
{
string s = turnToB(i * i);
if(check(s)) cout << turnToB(i) << " " << s << endl;
}
return 0;
}