USACO/Palindromic Squares

USACO/Palindromic Squares

Posted by dym on February 15, 2014

本文迁移自老博客,原始链接为 https://seven.blog.ustc.edu.cn/usacopalindromic-squares/

给定一个进制B(2<=B<=20,由十进制表示),输出所有的大于等于1小于等于300(十进制下)且它的平方用B进制表示时是回文数的数。 解法:枚举加字符串转换操作

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;

string change(int a , int b)
{
    char tmp[20];
    string hi;
    int t = 0;
    int ans[20];
    while(1)
    {
        ans[t++] = a % b;
        a = a / b;
        if(a == 0)
            break;
    }
    for(int i = t - 1; i >= 0;i--)
    {
        if(ans[i] <= 9)
            tmp[t - 1 -i] =  ans[i] + '0';
        else
        {
            tmp[t - 1 -i] = 'A' + ans[i] - 10;
        }
    }
    tmp[t] = '\0';
    hi = string(tmp);
    return hi;
}
bool isp(string a)
{
    int len = a.size();
    for(int i = 0; i < len; i++)
    {
        if(a[i] != a[len - i -1])
            return false;
    }
    return true;
}
int main() {
    ofstream fout ("palsquare.out");
    ifstream fin ("palsquare.in");
    int n;
    fin>>n;
    for(int i = 1 ;i <= 300 ;i++ )
    {
        if(isp(change(i * i , n)))
            fout<<change(i , n)<<" "<<change(i * i , n)<<endl;
    }
    return 0;
}