题解 | HJ99#自守数#
自守数
https://www.nowcoder.com/practice/88ddd31618f04514ae3a689e83f3ab8e
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//按字符串处理
int i = scanner.nextInt();
int num = 0;
for (int j = 0; j <= i; j++) {
if (IsZishouNum(j)) {
num++;
}
}
System.out.println(num);
}
public static boolean IsZishouNum(int i) {
String str = Integer.toString(i);
int length = str.length();//位数
// 9376^2 = 87909376
// int pow = (int)Math.pow(parseInt, 2);
double sqrt = Math.pow(i, 2);
String string = Integer.toString((int)sqrt);
String substring = string.substring(string.length() - length);
return substring.equals(str);
}
}
