Palindromic Squares

本文介绍了一个程序,该程序接收一个指定的基数B(2≤B≤20),并找出所有整数N(1≤N≤300),使得N的平方在基数B下表示为回文数。此外,程序还会输出这些回文数及其对应的平方。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



简单,不解释。
代码实现:
View Code
 1 /*
2 ID:10239512
3 PROG:palsquare
4 LANG:C++
5 */
6
7 #include<iostream>
8 #include<cstring>
9 #include<fstream>
10 //#define fout cout
11 //#define fin cin
12 using namespace std;
13 ifstream fin("palsquare.in");
14 ofstream fout("palsquare.out");
15
16 int a[100],b[100]={0};
17 int c;
18
19 bool dfs(int k){
20 int i;
21 for(i=1;i<=k/2;i++)
22 if(a[i]!=a[k-i+1])
23 return 0;
24 return 1;
25 }
26
27 void print(int c){
28 if(c<10) {fout<<c;return ;}
29 switch(c)
30 {
31 case 10: fout<<'A';break;
32 case 11: fout<<'B';break;
33 case 12: fout<<'C';break;
34 case 13: fout<<'D';break;
35 case 14: fout<<'E';break;
36 case 15: fout<<'F';break;
37 case 16: fout<<'G';break;
38 case 17: fout<<'H';break;
39 case 18: fout<<'I';break;
40 case 19: fout<<'A';break;
41 }
42
43 }
44
45 int main()
46 {
47 fin>>c;
48 int i,j,k,l;
49 for(i=1;i<=300;i++)
50 {
51 j=i*i;
52 k=1;
53 while(j>0)
54 {
55 a[k]=j%c;
56 k++;
57 j=j/c;
58 }
59 if(dfs(k-1))
60 {
61 j=1;
62 l=i;
63 while(l>0)
64 {
65 b[j]=l%c;
66 j++;
67 l=l/c;
68 }
69 j--;
70 while(j>=1)
71 {
72 print(b[j]);
73 j--;
74 }
75 fout<<" ";
76 k--;
77 while(k>=1)
78 {
79 print(a[k]);
80 k--;
81 }
82 fout<<endl;
83
84 }
85 //system("pause");
86 }
87 return 0;
88
89 }
90


转载于:https://www.cnblogs.com/noip/archive/2012/02/29/2374564.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值