USACO 1.2 Palsquare

题目连接

http://train.usaco.org/usacoprob2?a=ioYmQLbYXvn&S=palsquare

Description

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.

Input

A single line with B, the base (specified in base 10).

Output

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. NOTE WELL THAT BOTH INTEGERS ARE IN BASE B!

Sample Input

10

Sample Output

1 1
2 4
3 9
11 121
22 484
26 676
101 10201
111 12321
121 14641
202 40804
212 44944
264 69696

题意

给出一个进制(小于20)判断其平方数在此进制下是否为回文数,如果是的话输出这个数字在该进制下的值以及其平方数在此进制下的值。

题解

水题,大于10的时候转换一下即可,代码要写得简洁凝练。

代码

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
/*
ID: hyson601
PROG: palsquare
LANG: C++11
*/
#include <iostream>
using namespace std;
int now;
char numa[10000],numb[10000];
int n;
bool check(int i){
int length_i=0,length_now=0;
while(i){
char c;
int judge=i%n;
if(judge>=10){
c=judge-10+'A';
}
else{
c='0'+judge;
}
numa[length_i]=c;
length_i++;
i/=n;
}
numa[length_i]='\0';
int temp = now;
while(temp){
char c;
int judge=temp%n;
if(judge>=10){
c=judge-10+'A';
}
else{
c='0'+judge;
}
numb[length_now]=c;
length_now++;
temp/=n;
}
numb[length_now]='\0';
for(int j=0;j<length_i/2;++j){
char to_s=numa[j];
numa[j]=numa[length_i-1-j];
numa[length_i-1-j]=to_s;
}
for(int j=0;j<length_now/2;++j){
if(numb[j]!=numb[length_now-1-j])
return false;
}
return true;
}
int main(){
freopen("palsquare.in","r",stdin);
freopen("palsquare.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=300;++i){
now=i*i;
if(check(i)){
printf("%s %s\n",numa,numb);
}
}
return 0;
}