Description
Palindromes are numbers that read the same forwards as backwards. Like 12321.
Given a number base B (2 <= B <= 20), calculate all the number N( N no large than 300) such that N^2 is palindromic when expressed in base B.
Pay attention, 'A', 'B', and so on represent the digits 10, 11, and so on.
Print both the number and its square in base B.
Input
A single line with B.
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.
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 44944264 69696
题目不难 大意就是找出B进制的回文数,注意x和x^2都要回文数表示
#include<stdio.h> int main() { int a[20],c[20]; int b,i,j,k,q,p; scanf("%d",&b); for(i=1;i<300;i++) { int x,y,flag=1; x = i*i; for(j=0;x!=0;j++) { a[j] = x%b; x = x/b; } j--; for(k=0;j>k;j--,k++) if(a[j]!=a[k]) { flag = 0; break; } if(flag==1) { q = i; for(j=0;q!=0;j++) { p = q%b; c[j] = p; q = q/b; } j--; while(j>=0) { if(c[j]>9) printf("%c",c[j]+55); else printf("%c",c[j]+48); j--; } printf(" "); x = i*i; for(j=0;x!=0;j++){ y = x%b; if(y>9) printf("%c",y+55); else printf("%c",y+48); x = x/b; } printf("\n"); } } }