usaco Palindromic Squares

来源:互联网 发布:网络李逵劈鱼技巧 编辑:程序博客网 时间:2024/06/06 01:25
<pre name="code" class="cpp"><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">我用了一个字符数组分别保存i和i*i的B进制下的数。对于是否为回文数从数组前后一直判断到中间就行了。</span>

/*ID: jinbo wuLANG: C++PROB: palsquare*/#include<bits/stdc++.h>using namespace std;char s[50];int l;int n;stack<char> s1;void change(int n,int temp){char c; l=0;while(temp){int t=temp%n;if(t<10)s1.push(48+t);else s1.push('A'+t-10);temp/=n;}while(!s1.empty()){s[l++]=s1.top();s1.pop();}s[l]='\0';}bool is(){for(int i=0;i<=(l-1)/2;i++){if(s[i]!=s[l-1-i])return false;}return true;}void solve(){int temp;for(int i=1;i<=300;i++){temp=i*i;change(n,temp);if(is()){ change(n,i); printf("%s ",s); change(n,temp); puts(s);    }}}int main(){    freopen("palsquare.in","r",stdin);    freopen("palsquare.out","w",stdout);scanf("%d",&n);solve();}

0 0