[USACO 1.2.4]回文平方数

地址:http://hustoj.sinaapp.com/problem.php?id=1823

涉及到进制转换,要求输出在某进制下的回文数,用字符串处理会方便一些

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 char a[400];
 5 
 6 void swap()
 7 {
 8     int l=(int)strlen(a);
 9     int p1=0,p2=l-1;
10     int t;
11     while(p1<p2)
12     {
13         t=a[p1];
14         a[p1++]=a[p2];
15         a[p2--]=t;
16     }
17 }
18 
19 void convert(int x,int b)
20 {
21     int i=0,n;
22     memset(a,0,400*sizeof(char));
23     n=x;
24     while(n!=0)
25     {
26         if(n%b<10) a[i]=n%b+48;
27         else a[i]=n%b+55;
28         n=n/b;
29         i++;
30     }
31     a[i]='\0';
32 }
33 
34 int main()
35 {
36     int b,i=1,l,p1,p2,flag;
37     char t[400];
38     scanf("%d",&b);
39     for(i=1;i<=300;i++)
40     {
41         flag=1;
42         convert(i*i,b);
43         strcpy(t,a);
44         l=(int)strlen(t);
45         p1=0;
46         p2=l-1;
47         while(p1<p2)
48         {
49             if(t[p1]!=t[p2]) {flag=0;break;}
50             p1++;
51             p2--;
52         }
53         if(flag)
54         {
55             convert(i,b);
56             swap();
57             printf("%s %s\n",a,t);
58         }
59     }
60     return 0;
61 }

 

posted @ 2013-01-22 13:36  tjsuhst  阅读(409)  评论(0)    收藏  举报