java代码
c++语法还不太熟悉,就用Java随便写写把。。。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int B;
Scanner scanner = new Scanner(System.in);
B = scanner.nextInt();
for (int i = 1; i <= 300; i++) {
int num = i * i;
String s = convert(num, B);
boolean res = judge(s);
if (res) {
System.out.println(convert(i, B) + ' ' + s);
}
}
}
public static boolean judge(String s) {
String temp = new StringBuilder(s).reverse().toString();
if (temp.equals(s)) {
return true;
}
return false;
}
public static String convert(int num, int B) {
char[] map = {'0','1','2','3','4','5',
'6','7','8','9','A',
'B','C','D','E','F',
'G','H','I','J'};
String ans = "";
while (num > 0) {
ans += map[num % B];
num /= B;
}
return new StringBuilder(ans).reverse().toString();
}
}