今天状态不好,写的代码很烂,有时间重写,思路就不清楚呢,这么简单的题,此题与浙大PAT上某题类似
/* ID:linyvxi1 PROB:palsquare LANG:C++ */ #include <stdio.h> #include <string.h> #include <stack> #include <stdlib.h> using namespace std; int B; char str1[100]; char str2[100]; char str3[100]; bool check(int n) { memset(str1,'\0',sizeof(str1)); memset(str2,'\0',sizeof(str2)); memset(str3,'\0',sizeof(str3)); int i=0,j=0; while(n){ if(n%B>=10) str1[i]=n%B-10+'A'; else str1[i]=n%B+'0'; n/=B; i++; } for(i=strlen(str1)-1;i>=0;i--){ str2[j]=str1[i]; j++; }//reverse if(strcmp(str1,str2)==0) return true; return false; } int main() { FILE* fin=fopen("palsquare.in","r"); FILE* fout=fopen("palsquare.out","w"); fscanf(fin,"%d",&B); int i; for(i=1;i<=300;i++){ if(check(i*i)){ int j=0,tem=i;; while(tem){ if(tem%B>=10) str3[j]=tem%B-10+'A'; else str3[j]=tem%B+'0'; tem/=B; j++; } for(j=strlen(str3)-1;j>=0;j--) fprintf(fout,"%c",str3[j]); fprintf(fout," %s\n",str2); } } }
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