USACO1.23Palindromic Squares

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

给你个N 让你求n进制数的平方为回文串的数 十进制下300以内
View Code
 1 /*
 2  ID: your_id_here
 3  LANG: C++
 4  TASK: palsquare
 5  */
 6 #include <iostream>
 7 #include<cstdio>
 8 #include<string.h>
 9 using namespace std;
10 int n;
11 char pa[101],sq[111];
12 void base(int x,int y)
13 {
14     int i = 0,j = 0,t,k,flag = 1;
15     while(x)
16     {
17         t = x%n;
18         if(t>=10)
19         pa[++i] = t-10+'A';
20         else
21         pa[++i] = t+'0';
22         x = x/n;
23     }
24     while(y)
25     {
26         t = y%n;
27         if(t>=10)
28         sq[++j] = t-10+'A';
29         else
30         sq[++j] = t+'0';
31         y = y/n;
32     }
33     for(k = 1; k <= j/2 ;k++)
34     {
35         if(sq[k]!=sq[j-k+1])
36         {
37             flag = 0;
38             break;
39         }
40     }
41     if(flag)
42     {
43        for(k = i; k >= 1 ; k--)
44        printf("%c", pa[k]);
45        printf(" ");
46        for(k = j; k >= 1 ; k--)
47        printf("%c", sq[k]);
48        puts("");
49     }
50 }
51 int main()
52 {
53     freopen("palsquare.in","r",stdin);
54     freopen("palsquare.out","w",stdout);
55     int i,j;
56     scanf("%d", &n);
57     for(i = 1; i <= 300 ; i++)
58     {
59         base(i,i*i);
60     }
61     fclose(stdin);
62     fclose(stdout);
63     return 0;
64 }
原文地址:https://www.cnblogs.com/shangyu/p/2651419.html