题解
【基础】小X转进制
1 条题解
-
0
#include <iostream> #include <string> #include <algorithm> using namespace std; // 将十进制数字num转为 base进制字符串 string convert(long long num, int base) { char dict[] = "0123456789ABCDEF"; string res; if (num == 0) { return "0"; } while (num > 0) { res += dict[num % base]; num = num / base; } reverse(res.begin(), res.end()); // 取余得到逆序,反转 return res; } // 判断是否回文 bool isPalindrome(string s) { int l = 0; int r = s.size() - 1; while (l < r) { if (s[l] != s[r]) return false; l++; r--; } return true; } int main() { int N, M; cin >> N >> M; int ans = 0; for (int x = 1; x <= N; x++) { long long square = (long long)x * x; string str = convert(square, M); if (isPalindrome(str)) { ans++; } } cout << ans << endl; return 0; }
- 1