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. NOTE WELL THAT BOTH INTEGERS ARE IN BASE B!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
/*
ID: youqihe1
PROG: palsquare
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include<string.h>
using namespace std;
int pal(char A[])
{
int len=strlen(A),i,j;
for(i=0;i<len/2;i++)
if(A[i]!=A[len-i-1])
return 0;
return 1;
}
void quare(int n,int b,char C[])
{
int i=0;
char B[25];
memset(B,'\0',sizeof(B));
while(n)
{
int k=n%b;
if(k<10)
B[i++]=k+'0';
else
{
if(k==10) B[i++]='A';
if(k==11) B[i++]='B';
if(k==12) B[i++]='C';
if(k==13) B[i++]='D';
if(k==14) B[i++]='E';
if(k==15) B[i++]='F';
if(k==16) B[i++]='G';
if(k==17) B[i++]='H';
if(k==18) B[i++]='I';
if(k==19) B[i++]='J';
}
n/=b;
}
int t=0,j;
for(j=i-1;j>=0;j--,t++)
C[j]=B[t];
}
int main() {
FILE *fin = fopen ("palsquare.in", "r");
FILE *fout = fopen ("palsquare.out", "w");
int N;
fscanf(fin,"%d",&N);
int i,j;
for(i=1;i<=300;i++)
{
char B[25],C[25];
memset(B,'\0',sizeof(B));
memset(C,'\0',sizeof(C));
quare(i*i,N,B);
if(pal(B))
{
quare(i,N,C);
// printf("%d ",i);
// printf("%s %s\n",C,B);
fprintf(fout,"%s %s\n",C,B);
}
}
return 0;
}
同样的,还有这题
Mario Cruz (Colombia) & Hugo Rickeboer (Argentina)
A number that reads the same from right to left as when read from left to right is called a palindrome. The number 12321 is a palindrome; the number 77778 is not. Of course, palindromes have neither leading nor trailing zeroes, so 0220 is not a palindrome.
The number 21 (base 10) is not palindrome in base 10, but the number 21 (base 10) is, in fact, a palindrome in base 2 (10101).
Write a program that reads two numbers (expressed in base 10):
- N (1 <= N <= 15)
- S (0 < S < 10000)
Solutions to this problem do not require manipulating integers larger than the standard 32 bits.
PROGRAM NAME: dualpal
INPUT FORMAT
A single line with space separated integers N and S.
SAMPLE INPUT (file dualpal.in)
3 25
OUTPUT FORMAT
N lines, each with a base 10 number that is palindromic when expressed in at least two of the bases 2..10. The numbers should be listed in order from smallest to largest.SAMPLE OUTPUT (file dualpal.out)
26
27
28
也只要一个个判断即可
/*
ID: youqihe1
PROG: dualpal
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include<string.h>
using namespace std;
int pal(char A[])
{
int len=strlen(A),i,j;
for(i=0;i<len/2;i++)
if(A[i]!=A[len-i-1])
return 0;
return 1;
}
void quare(int n,int b,char C[])
{
int i=0;
char B[30];
memset(B,'\0',sizeof(B));
while(n)
{
int k=n%b;
B[i++]=k+'0';
n/=b;
}
int t=0,j;
for(j=i-1;j>=0;j--,t++)
C[j]=B[t];
}
int main() {
FILE *fin = fopen ("dualpal.in", "r");
FILE *fout = fopen ("dualpal.out", "w");
int N,S;
fscanf(fin,"%d %d",&N,&S);
int i,j,k,num=0;
for(i=S+1;num<N;i++)
{
int sum=0;
for(k=2;k<=10;k++)
{
char B[30];
memset(B,'\0',sizeof(B));
quare(i,k,B);
if(pal(B))
sum++;
if(sum==2)
{
fprintf(fout,"%d\n",i);
num++;
break;
}
}
}
return 0;
}