题目描述
模拟 + 暴力
(暴力枚举)
直接转换为给定进制,判断是否为回文
时间复杂度
$O(n^2)$
C++ 代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 30;
int base;
vector<int> getChange(int num, int base) {
vector<int> ans;
while (num) {
ans.push_back(num % base);
num /= base;
}
reverse(ans.begin(), ans.end());
return ans;
}
bool isParali(vector<int> &s) {
int length = s.size();
if (length == 1) {
return true;
}
for (int i = 0; i < length / 2; i++) {
if (s[i] != s[length - 1 - i]) {
return false;
}
}
return true;
}
void pr(vector<int> &s) {
for (int i = 0; i < s.size(); i++) {
if (s[i] < 10) {
cout << s[i];
} else {
char x = s[i] % 10 + 'A';
cout << x;
}
}
}
int main() {
cin >> base;
for (int i = 1; i <= 300; i++) {
vector<int> arr = getChange(i * i, base);
vector<int> s = getChange(i, base);
if (isParali(arr)) {
pr(s);
cout << " ";
pr(arr);
cout << endl;
}
}
return 0;
}