Advertisement
mickypinata

USACO-T008: Palindromic Square

Sep 20th, 2021
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. /*
  2. ID: mickyta1
  3. TASK: palsquare
  4. LANG: C++
  5. */
  6.  
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9.  
  10. string baseNumber(int x, int base){
  11.     string ans = "";
  12.     while(x > 0){
  13.         int newDigit = x % base;
  14.         if(newDigit >= 10){
  15.             ans = (char)('A' + newDigit - 10) + ans;
  16.         } else {
  17.             ans = (char)('0' + newDigit) + ans;
  18.         }
  19.         x /= base;
  20.     }
  21.     return ans;
  22. }
  23.  
  24. bool isPalindrome(string &str){
  25.     int len = str.size();
  26.     for(int i = 0; i < len / 2; ++i){
  27.         if(str[i] != str[len - i - 1]){
  28.             return false;
  29.         }
  30.     }
  31.     return true;
  32. }
  33.  
  34. int main(){
  35.  
  36.     freopen("palsquare.in", "r", stdin);
  37.     freopen("palsquare.out", "w", stdout);
  38.  
  39.     int base;
  40.     scanf("%d", &base);
  41.     for(int i = 1; i <= 300; ++i){
  42.         string i2baseB = baseNumber(i * i, base);
  43.         if(isPalindrome(i2baseB)){
  44.             cout << baseNumber(i, base) << ' ' << i2baseB << '\n';
  45.         }
  46.     }
  47.  
  48.     fclose(stdin);
  49.     fclose(stdout);
  50.  
  51.     return 0;
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement