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.
10
1 1 2 4 3 9 11 121 22 484 26 676 101 10201 111 12321 121 14641 202 40804 212 44944 264 69696
刚开始根本就没看清题,只考虑了10进制及以内的,后来发现是一直到20进制,之前在写时还在想怎么把数组和它的大小都传过来,真的好蠢!
改过后的代码(参考了一下网上的):
/* ID: PROG: palsquare LANG: C++ */ #include#include using namespace std; int N,l,L,x[50],y[50]; //用此函数转化进制 void trans(int n){ int m; m=n; l=0; while(m){x[++l]=m%N;m/=N;} m=n*n; L=0; while(m){y[++L]=m%N;m/=N;} } //用此函数判断是否为回文 int judge(){ for(int i=1;i >N; //对于10进制及以内进制的数,eg.x[4]=4;c[4]=4;(不变) //对于10进制以上的数,eg.x[4]=10;c[10]=A,可正确表示 char c[20]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J'}; for(int i=1;i<=300;i++){ trans(i); if(judge()){ while(l){ cout< 之前的错代码(条理很不清):
/* ID: PROG: palsquare LANG: C++ */ #include#include using namespace std; int N; int judge(int n){ int t=n*n; int s[100],str[100]; int rem,i=0; //把t转化成N进制,并存到数组s中 do{ rem=t%N; t/=N; s[i++]=rem; }while(t!=0); int flag=1; for(int j=0;j>N; int rem,j=0; for(int i=1;i<=300;i++){ judge(i); } return 0; }