Section 1.2 - Palindromic Squares

 1  #include  < iostream >
 2 
 3  using   namespace  std;
 4 
 5  // convert a number to string in base b
 6  string  num2string( int  n,  int  b)
 7  {
 8       string  s;
 9       while  (n)
10      {
11          s  +=   " 0123456789ABCDEFGHIJ " [n  %  b];
12          n  /=  b;
13      }
14       for  (unsigned i  =   0 ; i  <  s.size() /   2 ; i ++ )
15          swap(s[i], s[s.size()  -  i  -   1 ]);
16       return  s;
17  }
18 
19  bool  isPalindrome( const   string   &  s)
20  {
21       for  (unsigned i  =   0 ; i  <  s.size()  /   2 ; i ++ )
22           if  (s[i]  !=  s[s.size()  -  i  -   1 ])
23               return   false ;
24       return   true ;
25  }
26 
27  int  main()
28  {
29      freopen( " palsquare.in " " r " , stdin);
30      freopen( " palsquare.out " " w " , stdout);
31 
32       int  b;
33      cin  >>  b;
34       for  ( int  i  =   1 ; i  <=   300 ; i ++ )
35           if  (isPalindrome(num2string(i  *  i, b))  ==   true )
36              cout  <<  num2string(i, b)  <<   '   '   <<  num2string(i  *  i, b)  <<  endl;
37 
38       return   0 ;
39  }
40 

你可能感兴趣的:(Section 1.2 - Palindromic Squares)