Palindromic Squares

View Code
 1 #include<iostream>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<cstdio>
 5 #include<cstdlib>
 6 #include<cstring>
 7 
 8 using namespace std;
 9 
10 int n;
11 char b[101];
12 char bf[101];
13 
14 
15 void change(int i,int k)
16 {
17     int q=i,e=0,y;
18     if(k<=10)
19     {  
20        while(q!=0)
21        {
22           y=q%k;
23           q=q/k;
24           b[e]='0'+y;
25           e++;
26        }
27     }
28     else if(k>=11)
29     {
30        while(q!=0)
31        {
32           y=q%k;
33           q=q/k;
34           if(y<10) b[e]=y+'0';
35           else
36           b[e]=y-10+'A';
37           e++;
38        }
39     }
40     for(int i=0;i<e;i++)
41     {
42        bf[i]=b[e-i-1];
43     }
44 }
45 
46 
47 
48 bool check(char bf[])
49 {
50     char str[101];;
51     strcpy(str,bf);
52     int flag=true;
53     int i,j;
54     int long1=strlen(str);
55     for(i=0,j=long1-1;i<=j;i++,j--)
56     {
57         if(str[i]!=str[j])
58         {
59            return false;
60            break;
61         }
62     }
63     return true;
64 }
65 
66 
67 int main( )
68 {
69     freopen("palsquare.in","r",stdin);
70     freopen("palsquare.out","w",stdout);
71     cin>>n;
72     char r1[101],r2[101];
73     for(int i=1;i<=300;i++)
74     {
75         for(int j=0;j<=100;j++)
76         {
77            b[j]='\0';
78            bf[j]='\0';
79         }
80         change(i,n);
81         strcpy(r1,bf);
82         change(i*i,n);
83         strcpy(r2,bf);
84         bool ok2=check(r2);
85         if(ok2==true)
86         cout<<r1<<" "<<r2<<endl;
87     }  
88     return 0;
89 }

 

Palindromic Squares
Rob Kolstad

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
这道题主要是考察进制的转换,还有就是超过10的进制的转换是y-10+'A',就这样了,哦,不能给string 赋值char*
posted @ 2012-07-14 20:49  kaisadadi  阅读(148)  评论(0编辑  收藏  举报