自守数

自守数是指一个数的平方的尾数等于该数自身的自然数。例如:25^2 = 625,76^2 = 5776,9376^2 = 87909376。请求出n以内的自守数的个数

 接口说明


/*
功能: 求出n以内的自守数的个数


输入参数:
int n

返回值:
n以内自守数的数量。
*/

 public static int CalcAutomorphicNumbers( int n)

{
/*在这里实现功能*/

return 0;
}

输入描述:

int型整数

输出描述:

n以内自守数的数量。

输入例子:

2000
输出例子:
8
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            int n = in.nextInt();
            System.out.print(findNum(n));
            
        }
    }

    private static int findNum(int n) {
        int squl = 0;
        int num = 1; // 0是自守数
        for(int i = 1; i <= n; i++) {
            squl = i * i;
            int yu = 0;
            int result = 0;
            int count  = 0;
            while(squl > 0) {
                yu = squl % 10;
                result +=  yu * Math.pow(10, count);
                if(result == i) {
                    num ++;
                    break;
                }
                count ++;
                squl = squl / 10;
            }
        }
        
        return num;
    }
}
             
            
原文地址:https://www.cnblogs.com/zywu/p/5815410.html