usaco-palsquare

没什么动力刷刷水题。。

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


代码
 1 /*
 2 
 3 ID:doubles3
 4 PROB:palsquare
 5 LANG:C++
 6 
 7 */
 8 
 9 
10 
11 #include <iostream>
12 #include <cstdio>
13 #include <memory.h>
14 using namespace std;
15 
16 bool judge(int b,int base)
17 {
18 
19     int tmp[2000];
20     int a=b*b;
21     int p=0;
22     while(a)
23     {
24         tmp[p++]=a%base;
25         a/=base;
26     }
27 
28     for(int i=p-1;i>=(p/2);i--)
29     {
30         if(tmp[i] != tmp[p-i-1])return false;
31     }
32 
33     int org[2000]={0};
34     int po=0;
35     while(b)
36     {
37         org[po++]=b%base;
38         b/=base;
39     }
40     for(int i=po-1;i>=0;i--)
41     {
42         if(org[i]>=10)
43         {
44             printf("%c",'A'+org[i]-10);
45         }
46         else
47             printf("%d",org[i]);
48     }
49     printf(" ");
50 
51 
52     for(int i=p-1;i>=0;i--)
53     {
54         if(tmp[i]>=10)
55         {
56             printf("%c",'A'+tmp[i]-10);
57         }
58         else
59             printf("%d",tmp[i]);
60     }
61     printf("
");
62 
63     return true;
64 
65 }
66 
67 int main()
68 {
69     freopen("palsquare.in","r",stdin);
70 
71     freopen("palsquare.out","w",stdout);
72     int base;
73     scanf("%d",&base);
74 
75 
76     for(int i=1;i<=300;i++)
77     {
78         judge(i,base);
79     }
80 
81 
82     return 0;
83 }
原文地址:https://www.cnblogs.com/doubleshik/p/3468096.html