题目描述
自守数是指一个数的平方的尾数等于该数自身的自然数。例如:25^2 = 625,76^2 = 5776,9376^2 = 87909376。请求出n以内的自守数的个数
代码实现
import java.util.Scanner;
/**
* @ Created with IntelliJ IDEA.
* @ClassName TestCalcAutomorphicNumbers
* @Description 功能: 求出n以内的自守数的个数
* @Author by
* @Date 2020/5/23 22:02
*/
public class TestCalcAutomorphicNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
int n = scanner.nextInt();
System.out.println(CalcAutomorphicNumbers(n));
}
}
private static int CalcAutomorphicNumbers(int n) {
int count = 0;
while(n >= 0) {
if(isnum(n)) {
count++;
}
n--;
}
return count;
}
private static boolean isnum(int n ) {
int ans = n;
int s = n * n;
while(n != 0) {
if(n % 10 != s % 10) {
return false;
}
n = n / 10;
s = s / 10;
}
System.out.println(ans);
return true;
}
}