小皮博客 | Xiaopi's Blog

12_USACO_Section-1.2_Palindromic-Squares-平方回文数

原文:

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

翻译:平方回文

回文数字是指从后往前读以及从前往后读都一样的数字。 12321是一个典型的回文。

给出一个进制B(2<=B<=20),打印出所有的整数N(1<=N<=300 该进制下的,如B=10,那么就是10进制),他自己本身
是回文数,他的平方也是回文数字,使用”A”, “B” 等字母表示其他进制下的”10”, “11”..以此类推。

程序名称:palsquare
输入格式
B,进制 单行。
输入样例(文件palsquare.in)

10

输出格式
每行两个整数,第一个是回文数本身,第二个是他的平方。
输出样本(文件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
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
ID: sunsetx1
PROG: palsquare
LANG: C++
*/
#include<iostream>
#include<fstream>

using namespace std;

int pals[300], length;
char pals_ch[100];

ofstream fout ("palsquare.out");
ifstream fin ("palsquare.in");

char alpha[20] = {'0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J' };

int is_palindromic(char ch[]) {
int j = length - 1;
for (int i = 0; i &lt; length; i++ ) {
if (ch[i] != ch[j])
return 0; // 由于最多只有3位,所以这么简单的写了。
j--;
}
return 1;
}

/** 将一个10进制数字转化成该进制下的表示*/
void hex(int num, int base) {
int x;
x = num % base;
num = num / base;
if (num != 0) {
// 递归的求hex
hex(num, base);
}
pals_ch[length] = alpha[x];
length++;
}

int main() {
int BASE;
fin &gt;&gt; BASE;
int squ;
for (int i = 1; i &lt; 300; i++) {
squ = i * i; length = 0;
hex(squ, BASE);
if (is_palindromic(pals_ch) == 1 ) {
length = 0;
hex(i, BASE);
for (int p = 0; p &lt; length; p++)
fout &lt;&lt; pals_ch[p];
fout &lt;&lt; " ";
length = 0;
hex(squ, BASE);
for (int p = 0; p &lt; length; p++)
fout &lt;&lt; pals_ch[p];
fout &lt;&lt; endl;
}
}
fin.close();
fout.close();
return 0;
}

结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
USER: 领 盛 [sunsetx1]
TASK: palsquare
LANG: C++

Compiling...
Compile: OK

Executing...
Test 1: TEST OK [0.000 secs, 3504 KB]
Test 2: TEST OK [0.000 secs, 3504 KB]
Test 3: TEST OK [0.000 secs, 3504 KB]
Test 4: TEST OK [0.011 secs, 3504 KB]
Test 5: TEST OK [0.000 secs, 3504 KB]
Test 6: TEST OK [0.000 secs, 3504 KB]
Test 7: TEST OK [0.000 secs, 3504 KB]
Test 8: TEST OK [0.000 secs, 3504 KB]

All tests OK.
Your program ('palsquare') produced all correct answers!
This is your submission #3 for this problem. Congratulations!

Here are the test data inputs:
------- test 1 ----
10
------- test 2 ----
2
------- test 3 ----
5
------- test 4 ----
11
------- test 5 ----
15
------- test 6 ----
18
------- test 7 ----
20
------- test 8 ----
3
Keep up the good work!
Thanks for your submission!

版权声明

本文标题:12_USACO_Section-1.2_Palindromic-Squares-平方回文数

文章作者:盛领

发布时间:2015年02月09日 - 17:55:25

原始链接:http://blog.xiaoyuyu.net/post/9ae1f180.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

如您有任何商业合作或者授权方面的协商,请给我留言:sunsetxiao@126.com

盛领 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!