usaco 1.2 Palindromic Squares
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.
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
题意:
比较难懂。。。求1~300(十进制下)的平方在2~20的进制下为回文串的数;
水题啊水题。。。
代码:
View Code
1 /* 2 ID: jings_h1 3 PROG: palsquare 4 LANG: C++ 5 */ 6 7 #include<iostream> 8 #include<fstream> 9 #include<string.h> 10 using namespace std; 11 char a[100]; 12 char b[100]; 13 char trans[25]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K'}; 14 bool istrue(char a1[],int times1){ 15 for(int j=0;j<times1;j++){ 16 if(a1[j]!=a1[times1-1-j]) 17 return false; 18 } 19 return true; 20 } 21 int main(){ 22 int n; 23 ofstream fout ("palsquare.out"); 24 ifstream fin ("palsquare.in"); 25 while(fin>>n){ 26 for(int i=1;i<=300;i++){ 27 memset(a,0,sizeof(a)); 28 memset(b,0,sizeof(b)); 29 int times=0; 30 int times2=0; 31 int temp=i; 32 // int flag=0; 33 while(temp!=0){ 34 a[times++]=trans[temp%n]; 35 temp=temp/n; 36 } 37 int temp1=i*i; 38 while(temp1!=0){ 39 b[times2++]=trans[temp1%n]; 40 temp1=temp1/n; 41 } 42 if(istrue(b,times2)){ 43 for(int k=times-1;k>=0;k--) 44 fout<<a[k]; 45 // cout<<" "; 46 fout<<" "<<b<<endl; 47 } 48 } 49 50 } 51 return 0; 52 }