意思简单明确,只需要将1-300的数逐一平方,再转换为b进制下的数,最后判断是否为回文数,是便输出b进制下的数和它的平方数即可
#include <iostream>
using namespace std;
int b, a[120], c[120], t2 = 0, t1 = 0;
void zz(int n) {
int x = n * n;
while (x > 0) {
a[++t1] = x % b;
x /= b;//将数据的平方数转化为b进制下的数并由高位到低位储存位数
}
for (int i = 1; i <= t1 / 2; i++) {
if (a[i] != a[t1 - i + 1])//如果对应数位不相等,则不是回文数
return ;
}
while (n > 0) {
c[++t2] = n % b;
n /= b;//将数据转化为b进制下的数并由高位到低位储存位数
}
for (int i = t2; i >= 1; i--) {//由低位到高位输出b进制下的数据
if (c[i] < 10)
cout << c[i];
else
printf("%c", c[i] - 10 + 'A');//当位数大于等于10时,需要用字母代替
}
cout << ' ';
for (int i = t1; i >= 1; i--) {//由低位到高位输出数据的平方数
if (a[i] < 10)
cout << a[i];
else
printf("%c", a[i] - 10 + 'A');//当位数大于等于10时,需要用字母代替
}
cout << endl;;
}
int main() {
cin >> b;
cout << "1 1" << endl;
for (int i = 2; i <= 300; i++) {
zz(i);
t1 = 0;
t2 = 0;
}
return 0;
}