例如:25^2 = 625,
76^2 = 5776,
9376^2 = 87909376。
观察自守数,发现 n在n*n的末尾
出现。所以可以用 n*n.endWith(“n”) 来判断是否是自守数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
while (sc.hasNext()){
int cnt=0;
int x=sc.nextInt();
for (int i = 0; i <= x; i++) {
if(isZS(i)){
cnt++;
}
}
System.out.println(cnt);
}
}
public static boolean isZS(int x){
int sq=x*x;
String str=String.valueOf(x);
return String.valueOf(sq).endsWith(str);
}
}

京公网安备 11010502036488号