ในบทช่วยสอนนี้ เราจะอภิบายโปรแกรมเพื่อหาจำนวนพาลินโดรมที่เป็นกำลังสองของพาลินโดรม
สำหรับเรื่องนี้ เราจะได้รับค่าสองค่า L และ R งานของเราคือหาจำนวน super palindromes ในช่วงที่กำหนด Super palindrome คือตัวเลขที่มันและกำลังสองของมันเป็นพาลินโดรมทั้งคู่
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
// checking if the number is a palindrome
bool if_palin(int x) {
int ans = 0;
int temp = x;
while (temp > 0) {
ans = 10 * ans + temp % 10;
temp = temp / 10;
}
return ans == x;
}
// returning the count of palindrome
int is_spalin(int L, int R) {
// Upper limit
int LIMIT = 100000;
int ans = 0;
for (int i = 0; i < LIMIT; i++) {
string s = to_string(i);
string rs = s.substr(0, s.size() - 1);
reverse(rs.begin(), rs.end());
string p = s + rs;
int p_sq = pow(stoi(p), 2);
if (p_sq > R)
break;
if (p_sq >= L && if_palin(p_sq))
ans = ans + 1;
}
// counting even length palindromes
for (int i = 0; i < LIMIT; i++) {
string s = to_string(i);
string rs = s;
reverse(rs.begin(), rs.end());
string p = s + rs;
int p_sq = pow(stoi(p), 2);
if (p_sq > R)
break;
if (p_sq >= L && if_palin(p_sq))
ans = ans + 1;
}
return ans;
}
int main() {
string L = "4";
string R = "1000";
printf("%d\n", is_spalin(stoi(L), stoi(R)));
return 0;
}เอาต์พุต
4