首页 > 代码库 > 【USACO 1.2】Palindromic Squares

【USACO 1.2】Palindromic Squares

进制转换,然后判断是否是回文

/*******************************************  TASK: palsquare  LANG: C++   Created Time: 2016年09月07日 星期三 21时18分46秒 *********************************/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;void bas(int x,int b,char ans[]){    int i;    for(i=0;x;i++){        int t=x%b;        x/=b;        if(t>9)ans[i]=t%10+‘A‘;        else ans[i]=t+‘0‘;    }    for(int j=0;j<i/2;j++)	swap(ans[j],ans[i-j-1]);}int ispal(char s[]){    int len=strlen(s);    for(int i=0;i<len;i++)        if(s[i]!=s[len-i-1])return 0;    return 1;}int main(){    freopen("palsquare.in","r",stdin);    freopen("palsquare.out","w",stdout);    int b;    cin>>b;    for(int i=1;i<=300;i++){        char s[20];	memset(s,0,sizeof s);        bas(i*i,b,s);        if(ispal(s)){	    char n[20];	    memset(n,0,sizeof n);	    bas(i,b,n);	    cout<<n<<" "<<s<<endl;	}    }    return 0;}

 

【USACO 1.2】Palindromic Squares