首页 > 代码库 > Palindromic Squares

Palindromic Squares

链接

分析:求出b进制以后在判是否为回文

技术分享
 1 /*    
 2     ID:wanghan
 3     PROB:palsquare
 4     LANG:C++
 5 */
 6 #include "iostream"
 7 #include "cstdio"
 8 #include "cstring"
 9 #include "string"
10 using namespace std;
11 int b;
12 bool judge(string s,int i,int j){
13     while(i<j){
14         if(s[i]==s[j]){
15             i++,j--;
16         }else{
17             return false;
18         }
19     }
20     return true;
21 }
22 string Rev(int num){
23     string t="";
24     while(num){
25         int z=num%b;
26         if(z>=10)
27             t+=(z-10)+A;
28         else
29             t+=z+0;
30         num/=b;
31     }
32     int len=t.length();
33     int i=0,j=len-1;
34     while(i<j){
35         swap(t[i],t[j]);
36         i++,j--;
37     }
38     return t;
39 }
40 int main()
41 {
42     freopen("palsquare.in", "r", stdin);  
43     freopen("palsquare.out", "w", stdout);
44     cin>>b;
45     //cout<<Rev(1001)<<endl;
46     for(int i=1;i<=300;i++){
47         int ans=i,cnt=i*i;
48         string h1=Rev(ans),h2=Rev(cnt);
49         int len2=h2.length()-1;
50         if(judge(h2,0,len2)){
51             cout<<h1<<" "<<h2<<endl;
52         }
53     }
54     return 0;
55 }
View Code

 

Palindromic Squares