Fork me on GitHub

P1206 回文平方数

https://www.luogu.org/problem/show?pid=1206#sub

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstdio> 
using namespace std;
int B,a[1000],b[1000],cnt=0,cnt2=0;
char c[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K'};
void zhuan(int x)
{
    while(x)
    {
        a[++cnt]=x%B;
        x/=B;
    }
}
void zhuan2(int x)
{
    while(x)
    {
        b[++cnt2]=x%B;
        x/=B;
    }
}
bool check(int x)
{
    bool flag=true;
    zhuan(x);
    int j=cnt; 
    for(int i=1;i<=cnt/2;i++)
    {
        if(a[i]!=a[j])
        {
            flag=false;
            break;
        }
        j--;
    }
    return flag;
}
int main()
{
    scanf("%d",&B);
    for(int i=1;i<=300;i++)
    {
        cnt=0,cnt2=0;
        int x;
        x=i*i;
        if(check(x)) 
        {
            zhuan2(i);
            for(int i=cnt2;i>=1;i--)
             printf("%c",c[b[i]]);
            printf(" ");
            for(int i=cnt;i>=1;i--)
             printf("%c",c[a[i]]);
            puts("");
        }
    }
    return 0;
}
posted @ 2017-03-15 18:04  primes  阅读(209)  评论(0编辑  收藏  举报