usaco Palindromic Squares (N进制下的回文串)

来源:互联网 发布:java snmp agent库 编辑:程序博客网 时间:2024/06/06 18:13


Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome.

Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that the square of N is palindromic when expressed in base B; also print the value of that palindromic square. Use the letters 'A', 'B', and so on to represent the digits 10, 11, and so on.

Print both the number and its square in base B.

PROGRAM NAME: palsquare

INPUT FORMAT

A single line with B, the base (specified in base 10).

SAMPLE INPUT (file palsquare.in)

10

OUTPUT FORMAT

Lines with two integers represented in base B. The first integer is the number whose square is palindromic; the second integer is the square itself. NOTE WELL THAT BOTH INTEGERS ARE IN BASE B!

SAMPLE OUTPUT (file palsquare.out)

1 12 43 911 12122 48426 676101 10201111 12321121 14641202 40804212 44944264 69696


题意:列出num=i*i,(i>0 &&i<3=300 ) ,num在B进制下,是回文串。。并输出。


此处wrong了N发,要注意的是,回文串10以上要用‘A',‘B’....代替。。。 日啊


/**   PROG:palsquare   LANG:C++   ID:zpc19951 **/#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;int b;int a[1111],c[1111];void work(int num){int i,j,p,l,r,len,len1;int num1,num2;num1=num;p=0;while(num1) {c[++p]=num1%b;num1=num1/b;}len=p;p=0;num2=num*num;while(num2) {a[++p]=num2%b;num2=num2/b;}len1=p;l=1;r=p;while(l<=r) {if(a[l]!=a[r]) break;l++;r--;}if(l>r) {for(i=len;i;i--) {if(c[i]>9) printf("%c",'A'+(c[i]-10));else printf("%d",c[i]);}printf(" ");for(i=1;i<=len1;i++) {if(a[i]>9) printf("%c",'A'+(a[i]-10));else printf("%d",a[i]);}printf("\n");}}int main(){  freopen("palsquare.in","r",stdin);freopen("palsquare.out","w",stdout);int i,j;scanf("%d",&b);for(i=1;i<=300;i++) {work(i);}return 0;} 



0 0
原创粉丝点击