本文主要是介绍usaco 1.2 Palindromic Squares(进制转化),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
考察进制转化 注意一些细节就可以了
直接上代码:
/*
ID: who jay
LANG: C++
TASK: palsquare
*/
#include<stdio.h>int x[20],xlen,y[20],ylen,B;void change(int n)
{int m;m=n;xlen=0;while(m){x[++xlen]=m%B;m/=B;}m=n*n;ylen=0;while(m){y[++ylen]=m%B;m/=B;}
}bool ispal()
{bool flag=1;int i,j;for(i=1,j=ylen; i<=j; i++,j--){if(y[i]!=y[j]){flag=0;return 0;break;}}if(flag)return 1;
}int main()
{FILE *fin = fopen ("palsquare.in", "r");FILE *fout = fopen ("palsquare.out", "w");char c[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J'};int i,j;fscanf(fin,"%d",&B);for(i=1; i<301 ;i++){change(i);if(ispal()){for(j=xlen; j>0 ;j--)fprintf(fout,"%c",c[x[j]]);fprintf(fout," ");for(j=ylen; j>0 ;j--)fprintf(fout,"%c",c[y[j]]);fprintf(fout,"\n");}}return 0;
}
这篇关于usaco 1.2 Palindromic Squares(进制转化)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!