Problem 1548 猪数.
样例的15进制1 1怎么没有了我挺秃然的
VC++AC|GUN++WA
思路
- 遍历1-300的每个数字:
① 将该数字的平方转换成B进制
② 将B进制的该数字的平方转换成字符串
③ 判断该字符串是否回文–是则输出(两个B)
代码
#include<cstdio>
#include<math.h>
#include<string.h>
using namespace std;
int change(int B, int n, char a[]){
int x, len=0;
int i = (int)(log((double)(n))/log((double)B)); //找到最高位
while(i>=0){ //转成字符串
x = n / pow((float)B, (float)i);
n -= x*pow((float)B, (float)i);
i--;
if(x<10)
a[len++] = x+'0';
else
a[len++] = x-10+'A';
}
return len;
}
int main(){
int B;
char two[15], self[15];
int len1, len2, temp;
while(scanf("%d", &B)!=EOF){
for(int n=1; n<=300; n++){
memset(two, '\0', sizeof(two));
memset(self, '\0', sizeof(self));
len1 = change(B, n*n, two);
temp = 0; //判断是否回文
for(int i=0; i<=len1/2; i++)
if(two[i]!=two[len1-1-i]){
temp = 1;
break;
}
if(temp==0){
len2 = change(B, n, self);
printf("%s %s\n", self, two);
}
}
}
return 0;
}