判断回文数+转换八进制

This commit is contained in:
LittleSheep 2024-05-26 14:43:39 +08:00
parent 89365408c7
commit 7387683604

29
palindrome-number/main.cc Normal file
View File

@ -0,0 +1,29 @@
#include <cmath>
#include <iostream>
using namespace std;
string dec2oct(int dec) {
string buffer;
while (dec) {
buffer = char(dec % 8 + '0') + buffer;
dec /= 8;
}
return buffer;
}
bool is_palindrome_number(string val) {
string reversed;
for (int i = val.size() - 1; i >= 0; i--)
reversed += val[i];
return reversed == val;
}
int main() {
int n;
cin >> n;
int sqaure_n = sqrt(n);
for (int i = 1; i <= sqaure_n; i++) {
if (is_palindrome_number(dec2oct(i * i)))
cout << i * i << " ";
}
}