Rob Kolstad
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.SAMPLE OUTPUT (file palsquare.out)
1 1 2 4 3 9 11 121 22 484 26 676 101 10201 111 12321 121 14641 202 40804 212 44944 264 69696
-------------------------------------------------------------------------------
/* ID: 551100k1 LANG: C++ TASK: palsquare */ #include<stdio.h> #include<iostream> using namespace std; string a,b; char s[10]; char d[30]="0123456789ABCDEFGHIJKLMN"; int check(){ int i; for(i=0;i<a.length();i++){ if(a[i]!=a[a.length()-i-1]) return 0; }return 1; } int main(){ freopen("palsquare.in","r",stdin); freopen("palsquare.out","w",stdout); int n,i,k,flag; scanf("%d",&n); for(i=1;i<=300;i++){ a.clear(),b.clear(); k=i*i,flag=1; while(k){ a=d[k%n]+a; k/=n; } k=i; while(k){ b=d[k%n]+b; k/=n; } if(check()) printf("%s ",b.c_str()),puts(a.c_str()); } }
沒有留言:
張貼留言